Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String query = ""
- + "CREATE TABLE IF NOT EXISTS `minigames`("
- + "`id` int NOT NULL,"
- + "`name` text,"
- + "`displayname` text,"
- + "PRIMARY KEY(id));"
- + "CREATE TABLE IF NOT EXISTS `minigamesrv`("
- + "`id` int NOT NULL,"
- + "`name` text,"
- + "`minigamename` text,"
- + "`serverip` text,"
- + "`path` text,"
- + "PRIMARY KEY(id));";
- instance.update(query);
- // --------------------------------------------------------
- private int tries;
- public Connection getSQL()
- {
- try
- {
- if(connection == null || connection.isClosed())
- {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbname + "?allowMultiQueries=true", user, pass);
- }
- }
- catch(Exception e)
- {
- System.err.println("! IMPORTANT ! Impossible de se connecter à la base de donnée !");
- tries++;
- }
- if(tries >= 10)
- {
- System.err.println("! Arrêt du plugin !");
- this.getProxy().getPluginManager().unregisterCommands(this);
- this.getProxy().getPluginManager().unregisterListeners(this);
- disable = true;
- }
- return connection;
- }
- @SuppressWarnings("unchecked")
- public <T> T getResult(Class<T> object, String query, String table, Object... args)
- {
- if(object.getName().equals(ResultSet.class.getName()))
- {
- try
- {
- Connection co = getSQL();
- PreparedStatement stmt = co.prepareStatement(query);
- for(int i = 1; i <= args.length; i++) stmt.setObject(i, args[i - 1]);
- ResultSet rs = stmt.executeQuery();
- return (T) rs;
- }
- catch(SQLException e) { return null; }
- }
- else
- {
- try
- {
- ResultSet rs = getResult(ResultSet.class, query, null, args);
- if(rs.next()) return (T) rs.getObject(table);
- return null;
- }
- catch(SQLException e) { return null; }
- }
- }
- public void update(String query, Object... args)
- {
- try
- {
- Connection co = getSQL();
- PreparedStatement stmt = co.prepareStatement("use "+dbname+";"+query);
- for(int i = 0; i < args.length; i++) stmt.setObject(i + 1, args[i]);
- stmt.executeUpdate();
- }
- catch(SQLException e) { e.printStackTrace(); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement