Advertisement
Guest User

Untitled

a guest
Jun 12th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. package com.ferdz.reevecredits.sql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.UUID;
  9.  
  10. import com.ferdz.reevecredits.ReeveCredits;
  11.  
  12. public class MySQL {
  13.  
  14.     private String HOST = "";
  15.     private String DATABASE = "";
  16.     private String USER = "";
  17.     private String PASSWORD = "";
  18.  
  19.     private Connection con;
  20.  
  21.     public MySQL(String host, String database, String user, String password) {
  22.         this.HOST = host;
  23.         this.DATABASE = database;
  24.         this.USER = user;
  25.         this.PASSWORD = password;
  26.  
  27.         connect();
  28.     }
  29.  
  30.     public void connect() {
  31.         System.out.println("[ReeveCredits] [MySQL] Trying to connect to the MySQL database");
  32.         try {
  33.             con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  34.             System.out.println("[ReeveCredits] [MySQL] The connection to MySQL was successful!");
  35.         } catch (SQLException e) {
  36.             System.out.println("[ReeveCredits] [MySQL] The connection to MySQL couldn't be made! reason: " + e.getMessage());
  37.         }
  38.     }
  39.  
  40.     public void close() {
  41.         try {
  42.             if (con != null) {
  43.                 con.close();
  44.                 System.out.println("[ReeveCredits] [MySQL] The connection to MySQL has ended successfully!");
  45.             }
  46.         } catch (SQLException e) {
  47.             System.out.println("[ReeveCredits] [MySQL] The connection couldn't be closed! reason: " + e.getMessage());
  48.         }
  49.     }
  50.  
  51.     public void update(String qry) {
  52.         try {
  53.             Statement st = con.createStatement();
  54.             st.executeUpdate(qry);
  55.             st.close();
  56.         } catch (SQLException e) {
  57.             connect();
  58.             System.err.println(e);
  59.         }
  60.     }
  61.  
  62.     public boolean hasConnection() {
  63.         if (con != null) {
  64.             return true;
  65.         }
  66.         return false;
  67.     }
  68.  
  69.     public ResultSet query(String qry) {
  70.         ResultSet rs = null;
  71.  
  72.         try {
  73.             Statement st = con.createStatement();
  74.             rs = st.executeQuery(qry);
  75.         } catch (SQLException e) {
  76.             connect();
  77.             System.err.println(e);
  78.         }
  79.         return rs;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement