Advertisement
Guest User

Untitled

a guest
May 15th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. private MySQLFile mySQLFile = System.getPlugin().getMySQLFile();
  2.  
  3.     private String host = "";
  4.     private String databse = "";
  5.     private  String user = "";
  6.     private String password = "";
  7.  
  8.     private Connection connection;
  9.  
  10.     public MySQL(String host,String database,String user,String password){
  11.         this.host = host;
  12.         this.databse = database;
  13.         this.user = user;
  14.         this.password = password;
  15.  
  16.         connect();
  17.     }
  18.  
  19.     public MySQL(String host,String database,String user){
  20.         this.host = host;
  21.         this.databse = database;
  22.         this.user = user;
  23.  
  24.         connect();
  25.     }
  26.  
  27.     public MySQL(){
  28.  
  29.         this.host = mySQLFile.getHost();
  30.         this.databse = mySQLFile.getDatabase();
  31.         this.user = mySQLFile.getUser();
  32.         this.password = mySQLFile.getPassword();
  33.  
  34.         connect();
  35.     }
  36.  
  37.     private void connect(){
  38.  
  39.         try {
  40.             Class.forName("com.mysql.jdbc.Driver");
  41.             connection = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+databse+"?autoReconnect=true",user,password);
  42.             Bukkit.getConsoleSender().sendMessage(System.PREFIX+"§aEs wurde eine Verbindung hergestellt.");
  43.             createTable();
  44.         } catch (Exception e) {
  45.             Bukkit.getConsoleSender().sendMessage( System.PREFIX+"§4Es gab einen Fehler!");
  46.             // e.printStackTrace();
  47.         }
  48.  
  49.     }
  50.  
  51.     public void disconnect(){
  52.         try {
  53.             if (connection != null){
  54.                 connection.close();
  55.                 Bukkit.getConsoleSender().sendMessage(System.PREFIX+"§aDie Verbindung wurde getrennt!");
  56.             }
  57.         } catch (SQLException ex){
  58.             ex.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     private void createTable(){
  63.         try {
  64.             connection.prepareStatement("CREATE TABLE IF NOT EXISTS rp (uuid VARCHAR(150), level INT(16))").executeUpdate();
  65.             Bukkit.getConsoleSender().sendMessage(System.PREFIX+"§aEs wurde eine Tabelle erstellt.");
  66.         } catch (SQLException e) {
  67.             Bukkit.getConsoleSender().sendMessage(System.PREFIX+"§4Es gab einen Fehler bei der erstellung der Tabelle!");
  68.             // e.printStackTrace();
  69.         }
  70.     }
  71.  
  72.     public Connection getConnection() {
  73.         return connection;
  74.     }
  75.  
  76.     public boolean isConnected(){
  77.         return (connection != null);
  78.     }
  79.  
  80.     public void update(String qry){
  81.  
  82.         try {
  83.             Statement st = connection.createStatement();
  84.             st.executeUpdate(qry);
  85.             st.close();
  86.         } catch (SQLException e) {
  87.             e.printStackTrace();
  88.         }
  89.     }
  90.  
  91.     public void updatePREPARE(String qry){
  92.  
  93.         try {
  94.             PreparedStatement st = connection.prepareStatement(qry);
  95.             st.executeUpdate(qry);
  96.         } catch (SQLException e) {
  97.             e.printStackTrace();
  98.         }
  99.     }
  100.  
  101.     public ResultSet getResult(String qry){
  102.         try {
  103.             PreparedStatement st = connection.prepareStatement(qry);
  104.             return st.executeQuery();
  105.         } catch (SQLException e) {
  106.             e.printStackTrace();
  107.         }
  108.         return null;
  109.     }
  110.  
  111.     public ResultSet query(String qry){
  112.         ResultSet rs = null;
  113.         try {
  114.             Statement st = connection.createStatement();
  115.             rs = st.executeQuery(qry);
  116.         } catch (SQLException e) {
  117.             e.printStackTrace();
  118.         }
  119.         return rs;
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement