· 8 years ago · Jan 09, 2018, 01:32 PM
1package fr.flashback083.config;
2
3import java.sql.DriverManager;
4import java.sql.SQLException;
5
6import com.mysql.jdbc.Connection;
7
8import fr.flashback083.proxy.CommonProxy;
9import net.minecraftforge.common.config.Configuration;
10
11public class SQL {
12
13 public static SQL sql;
14 public java.sql.Connection connection;
15 private String urlbase, host, database, user, pass;
16 Configuration cfg = CommonProxy.configbdd;
17
18 public SQL(String urlbase, String host, String database, String user, String pass){
19 this.urlbase = "jdbc:mysql://";
20 this.host = cfg.getCategory("Base de donnees").get("Host").getString();
21 this.database = cfg.getCategory("Base de donnees").get("Database").getString();
22 this.user = cfg.getCategory("Base de donnees").get("User").getString();
23 this.pass = cfg.getCategory("Base de donnees").get("MDP").getString();
24 }
25
26 //Création de la table si non existante lors du lancement serveur
27 public Connection connection() {
28 if(!this.isConnected()) {
29 try {
30 connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
31 System.out.println("connexion a la BDD reussi");
32 createTable();
33 } catch (SQLException var2) {
34 var2.printStackTrace();
35 }
36 }
37
38 return null;
39 }
40
41
42
43
44 //Simple déconnection
45 public Connection deconnection() {
46 if(this.isConnected()) {
47 try {
48 this.connection.close();
49 System.out.println("BDD deconnecter");
50 } catch (SQLException var2) {
51 var2.printStackTrace();
52 }
53 }
54
55 return null;
56 }
57
58
59 //Création de la table si non existante.
60 public void createTable() throws SQLException {
61
62
63 String gtm = "CREATE TABLE IF NOT EXISTS GTM"
64 + " (ID INTEGER default 0,"
65 + " PseudoV VARCHAR(20),"
66 + " Type VARCHAR(10),"
67 + " Nom VARCHAR(50),"
68 + " Objet VARCHAR(2500),"
69 + " Quantity INTEGER default 1,"
70 + " PokeD INTEGER default 0,"
71 + " PointsB INTEGER default 0,"
72 + " Etat INTEGER default 0,"
73 + " Date INTEGER,"
74 + " PseudoA VARCHAR(20),"
75 + " PRIMARY KEY (ID))";
76
77
78 java.sql.Statement stmt = this.connection.createStatement();
79
80 stmt.execute(gtm);
81
82
83 }
84
85
86
87
88 //Boolean
89 public boolean isConnected() {
90 return this.connection != null;
91 }
92
93
94}