Guest User

Untitled

a guest
Mar 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. private boolean mysql_init() {
  2.        
  3.         try {
  4.             new File("plugins" + File.separator + "authx60").mkdir();
  5.             File cfile = new File("plugins" + File.separator + "authx60" + File.separator + "config.yml");
  6.            
  7.             if(!cfile.exists())
  8.                 cfile.createNewFile();
  9.            
  10.             Configuration config = new Configuration(cfile);
  11.             config.load();
  12.            
  13.             String db_host = config.getString("database.host");
  14.             String db_user = config.getString("database.user");
  15.             String db_pass = config.getString("database.pass");
  16.             String db_name = config.getString("database.name");
  17.            
  18.             if(db_host == null)
  19.                 db_host = "localhost";
  20.            
  21.             if(db_user == null)
  22.                 db_user = "root";
  23.            
  24.             if(db_pass == null)
  25.                 db_pass = "";
  26.            
  27.             if(db_name == null)
  28.                 db_name = "minecraft";
  29.  
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.             con = DriverManager.getConnection("jdbc:mysql://"+db_host+"/"+db_name, db_user, db_pass);
  32.             return true;
  33.         } catch ( Exception e ) {
  34.             e.printStackTrace();
  35.             return false;
  36.         }
  37.     }
  38.    
  39.     public List<Map<String,String>> mysql_select(String query) {
  40.         mysql_init();
  41.         ResultSet rs = null;
  42.         List<Map<String, String>> result = new ArrayList<Map<String, String>>();
  43.         try {
  44.             Statement stmt = con.createStatement();
  45.             rs = stmt.executeQuery(query);
  46.             ResultSetMetaData rsmd = rs.getMetaData();
  47.  
  48.            
  49.             while (rs.next()) {
  50.                 Map<String, String> temp = new HashMap<String, String>();
  51.                
  52.                 for(int i = 1; i <= rsmd.getColumnCount(); i++)
  53.                     temp.put(rsmd.getColumnLabel(i), rs.getString(i));
  54.                 result.add(temp);
  55.             }
  56.             stmt.close();
  57.         } catch (SQLException e) {
  58.             e.printStackTrace();
  59.         }
  60.         mysql_disconnect();
  61.         con = null;
  62.         return result;
  63.     }
  64.    
  65.     public void mysql_update(String query) {
  66.         mysql_init();
  67.        
  68.         try {
  69.             Statement stmt = con.createStatement();
  70.             stmt.execute(query);
  71.             stmt.close();
  72.         } catch (SQLException e) {
  73.             e.printStackTrace();
  74.         }
  75.        
  76.         mysql_disconnect();
  77.         con = null;
  78.     }
  79.    
  80.    
  81.     private void mysql_disconnect() {
  82.         try {
  83.             con.close();
  84.         } catch(SQLException e) {
  85.             e.printStackTrace();
  86.         }
  87.     }
Add Comment
Please, Sign In to add comment