Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. package at.MaxPlays.NexxScoreboard.main;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10.  
  11.  
  12.  
  13. public class MySQL {
  14.  
  15.     private String HOST = "";
  16.     private String DATABASE = "";
  17.     private String USER = "";
  18.     private String PORT = "";
  19.     private String PASSWORD = "";
  20.    
  21.     private Connection con;
  22.    
  23.     public MySQL(String host, String database, String user, String password, String port){
  24.         this.HOST = host;
  25.         this.DATABASE = database;
  26.         this.USER = user;
  27.         this.PASSWORD = password;
  28.         this.PORT = port;
  29.     }
  30.    
  31.     public void connect(){
  32.         try{
  33.             con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE + "?autoreconnect=true", USER, PASSWORD);
  34.             System.out.println("[MySQL] Connection established");
  35.         }catch(SQLException e){
  36.             System.out.println("[MySQL] Connection failed! Error: " + e.getMessage());
  37.         }
  38.     }
  39.    
  40.     public void disconnect(){
  41.         try{
  42.             if(connected()){
  43.             con.close();
  44.             System.out.println("[MySQL] Disconnected");
  45.            
  46.             }
  47.        
  48.         }catch(SQLException e){
  49.             System.out.println("[MySQL] Error while disconnecting: " + e.getMessage());
  50.         }
  51.     }
  52.     public boolean connected(){
  53.         return con == null ? false : true;
  54.     }
  55.     public void update(String qry){
  56.         try {
  57.             Statement st = con.createStatement();
  58.             st.executeUpdate(qry);
  59.             st.close();
  60.         } catch (SQLException e) {
  61.             connect();
  62.             System.err.println(e);
  63.         }
  64.     }
  65.     public ResultSet querry(String qry){
  66.         ResultSet rs = null;
  67.         try {
  68.             Statement st = con.createStatement();
  69.             rs = st.executeQuery(qry);
  70.         } catch (SQLException e) {
  71.             connect();
  72.             System.err.println(e);
  73.         }
  74.         return rs;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement