Guest User

MySQL

a guest
Dec 25th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package com.codingwelt.citylife.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. public class MySQL {
  10.    
  11.    
  12.      private String HOST = "localhost";
  13.     private String DATABASE = "minecraft";
  14.     private String USER = "root";
  15.     private String PASSWORD = "";
  16.    
  17.     private Connection con;
  18.    
  19.     public MySQL(String host, String database, String user, String password) {
  20.             this.HOST = host;
  21.             this.DATABASE = database;
  22.             this.USER = user;
  23.             this.PASSWORD = password;
  24.            
  25.             connect();
  26.     }
  27.  
  28.     public void connect() {
  29.             try {
  30.                     con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  31.                     System.out.println("[CityLife] Die Verbindung zur MySQL wurde hergestellt!");
  32.             } catch (SQLException e) {
  33.                     System.out.println("[CityLife] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  34.             }
  35.     }
  36.    
  37.     public void close() {
  38.             try {
  39.                     if(con != null) {
  40.                             con.close();
  41.                             System.out.println("[CityLife] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  42.                     }
  43.             } catch (SQLException e) {
  44.                     System.out.println("[CityLife] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  45.             }
  46.     }
  47.    
  48.     public void update(String qry) {
  49.             try {
  50.                     Statement st = con.createStatement();
  51.                     st.executeUpdate(qry);
  52.                     st.close();
  53.             } catch (SQLException e) {
  54.                     connect();
  55.                     System.err.println(e);
  56.             }
  57.     }
  58.    
  59.     public ResultSet query(String qry) {
  60.             ResultSet rs = null;
  61.            
  62.             try {
  63.                     Statement st = con.createStatement();
  64.                     rs = st.executeQuery(qry);
  65.             } catch (SQLException e) {
  66.                     connect();
  67.                     System.err.println(e);
  68.             }
  69.             return rs;
  70.     }
  71. }
Add Comment
Please, Sign In to add comment