Oniels098

cock

Sep 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. package me.xericker.mysteryboxes.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import me.xericker.mysteryboxes.config.Config.RConfig;
  8. import me.xericker.mysteryboxes.config.ConfigMgr;
  9.  
  10. public class MySQL
  11. {
  12.   private static Connection connection;
  13.  
  14.   public static void connect()
  15.   {
  16.     if (!ConfigMgr.config.getBoolean("mysql.enabled")) {
  17.       return;
  18.     }
  19.     try
  20.     {
  21.       Class.forName("com.mysql.jdbc.Driver");
  22.      
  23.       String host = ConfigMgr.config.getString("mysql.host");
  24.       String datebase = ConfigMgr.config.getString("mysql.datebase");
  25.       String username = ConfigMgr.config.getString("mysql.username");
  26.       String password = ConfigMgr.config.getString("mysql.password");
  27.       connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + datebase + "?user=" + username + "&password=" + password + "");
  28.      
  29.       checkTables();
  30.     }
  31.     catch (Exception exception)
  32.     {
  33.       exception.printStackTrace();
  34.     }
  35.   }
  36.  
  37.   public static boolean columnExists(String table, String column)
  38.   {
  39.     try
  40.     {
  41.       ResultSet resultSet = query("SELECT " + column + " FROM " + table + ";");
  42.       return resultSet != null;
  43.     }
  44.     catch (Exception exception) {}
  45.     return false;
  46.   }
  47.  
  48.   public static void checkTables() {}
  49.  
  50.   public static void update(String query)
  51.   {
  52.     try
  53.     {
  54.       if ((connection == null) || (connection.isClosed())) {
  55.         connect();
  56.       }
  57.       PreparedStatement preparedStatement = connection.prepareStatement(query);
  58.       preparedStatement.execute();
  59.       preparedStatement.close();
  60.     }
  61.     catch (Exception exception)
  62.     {
  63.       exception.printStackTrace();
  64.     }
  65.   }
  66.  
  67.   public static ResultSet query(String query)
  68.   {
  69.     try
  70.     {
  71.       if ((connection == null) || (connection.isClosed())) {
  72.         connect();
  73.       }
  74.       PreparedStatement preparedStatement = connection.prepareStatement(query);
  75.       return preparedStatement.executeQuery();
  76.     }
  77.     catch (Exception exception)
  78.     {
  79.       exception.printStackTrace();
  80.     }
  81.     return null;
  82.   }
  83. }
Add Comment
Please, Sign In to add comment