daily pastebin goal
23%
SHARE
TWEET

MySQL

a guest Mar 17th, 2018 87 in 59 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.snieqz.bw.mysql;
  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.  
  9. import com.mysql.jdbc.PreparedStatement;
  10.  
  11. public class MySQL {
  12.  
  13.  
  14.     private static String HOST = "";
  15.     private static String DATABASE = "";
  16.     private static String USER = "";
  17.     private static String PASSWORD = "";
  18.    
  19.     private static 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 static void connect() {
  31.         try {
  32.             con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  33.             System.out.println("[MySQL] Die Verbindung zur MySQL wurde hergestellt!");
  34.         } catch (SQLException e) {
  35.             System.out.println("[MySQL] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  36.         }
  37.     }
  38.    
  39.     public void close() {
  40.         try {
  41.             if(con != null) {
  42.                 con.close();
  43.                 System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  44.             }
  45.         } catch (SQLException e) {
  46.             System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  47.         }
  48.     }
  49.    
  50.     public static void update(String qry) {
  51.         try {
  52.             Statement st = con.createStatement();
  53.             st.executeUpdate(qry);
  54.             st.close();
  55.         } catch (SQLException e) {
  56.             connect();
  57.             System.err.println(e);
  58.         }
  59.     }
  60.    
  61.     public static ResultSet query(String qry) {
  62.         ResultSet rs = null;
  63.        
  64.         try {
  65.             Statement st = con.createStatement();
  66.             rs = st.executeQuery(qry);
  67.         } catch (SQLException e) {
  68.             connect();
  69.             System.err.println(e);
  70.         }
  71.         return rs;
  72.     }
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top