· 7 years ago · Jan 03, 2019, 06:14 PM
1
2 String query = ""
3 + "CREATE TABLE IF NOT EXISTS `minigames`("
4 + "`id` int NOT NULL,"
5 + "`name` text,"
6 + "`displayname` text,"
7 + "PRIMARY KEY(id));"
8
9 + "CREATE TABLE IF NOT EXISTS `minigamesrv`("
10 + "`id` int NOT NULL,"
11 + "`name` text,"
12 + "`minigamename` text,"
13 + "`serverip` text,"
14 + "`path` text,"
15 + "PRIMARY KEY(id));";
16 instance.update(query);
17
18
19
20// --------------------------------------------------------
21
22
23
24 private int tries;
25 public Connection getSQL()
26 {
27 try
28 {
29 if(connection == null || connection.isClosed())
30 {
31 Class.forName("com.mysql.jdbc.Driver");
32 this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbname + "?allowMultiQueries=true", user, pass);
33 }
34 }
35 catch(Exception e)
36 {
37 System.err.println("! IMPORTANT ! Impossible de se connecter à la base de donnée !");
38 tries++;
39 }
40 if(tries >= 10)
41 {
42 System.err.println("! Arrêt du plugin !");
43 this.getProxy().getPluginManager().unregisterCommands(this);
44 this.getProxy().getPluginManager().unregisterListeners(this);
45 disable = true;
46 }
47 return connection;
48 }
49
50 @SuppressWarnings("unchecked")
51 public <T> T getResult(Class<T> object, String query, String table, Object... args)
52 {
53 if(object.getName().equals(ResultSet.class.getName()))
54 {
55 try
56 {
57 Connection co = getSQL();
58 PreparedStatement stmt = co.prepareStatement(query);
59 for(int i = 1; i <= args.length; i++) stmt.setObject(i, args[i - 1]);
60 ResultSet rs = stmt.executeQuery();
61 return (T) rs;
62 }
63 catch(SQLException e) { return null; }
64 }
65 else
66 {
67 try
68 {
69 ResultSet rs = getResult(ResultSet.class, query, null, args);
70 if(rs.next()) return (T) rs.getObject(table);
71 return null;
72 }
73 catch(SQLException e) { return null; }
74 }
75 }
76
77 public void update(String query, Object... args)
78 {
79 try
80 {
81 Connection co = getSQL();
82 PreparedStatement stmt = co.prepareStatement("use "+dbname+";"+query);
83 for(int i = 0; i < args.length; i++) stmt.setObject(i + 1, args[i]);
84 stmt.executeUpdate();
85 }
86 catch(SQLException e) { e.printStackTrace(); }
87 }