Guest User

Untitled

a guest
Sep 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. public class MySQL {
  2.  
  3.     private String HOST = "";
  4.     private String DATABASE = "";
  5.     private String USER = "";
  6.     private String PASSWORD = "";
  7.  
  8.     private Connection con;
  9.  
  10.     public MySQL(String host, String database, String user, String password) {
  11.         this.HOST = host;
  12.         this.DATABASE = database;
  13.         this.USER = user;
  14.         this.PASSWORD = password;
  15.  
  16.         connect();
  17.     }
  18.  
  19.     public void connect() {
  20.         try {
  21.             con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  22.             System.out.println("[TPLogin] Conectado ao MySQL com sucesso!");
  23.         } catch(SQLException e) {
  24.             System.out.println("[TPLogin] Erro ao se conectar ao MySQL, erro: " + e.getMessage());
  25.         }
  26.     }
  27.  
  28.     public void close() {
  29.         try {
  30.             if(con != null) {
  31.                 con.close();
  32.                 System.out.println("[TPLogin] Conectado ao MySQL com sucesso!");
  33.             }
  34.         } catch(SQLException e) {
  35.             System.out.println("[TPLogin] Erro ao se conectar ao MySQL, erro: " + e.getMessage());
  36.         }
  37.     }
  38.  
  39.     public void update(String qry) {
  40.         try {
  41.             Statement st = con.createStatement();
  42.             st.executeUpdate(qry);
  43.             st.close();
  44.         } catch(SQLException e) {
  45.             connect();
  46.             System.err.println(e);
  47.         }
  48.     }
  49.  
  50.     public ResultSet query(String qry) {
  51.         ResultSet rs = null;
  52.  
  53.         try {
  54.             Statement st = con.createStatement();
  55.             rs = st.executeQuery(qry);
  56.         } catch(SQLException e) {
  57.             connect();
  58.             System.err.println(e);
  59.         }
  60.         return rs;
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment