Guest User

Untitled

a guest
May 1st, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MySQL {
  4.    
  5.         private String host, database, user, password;
  6.         private int port;
  7.         private Connection con;
  8.  
  9.         public MySQL(String host, String database, String user, String password, int port) {
  10.             this.host = host;
  11.             this.database = database;
  12.             this.user = user;
  13.             this.password = password;
  14.             this.port = port;
  15.         }
  16.  
  17.         /**
  18.          * Verbindung aufbauen mit JDBC Driver
  19.          */
  20.         public void connect() {
  21.             try {
  22.                 con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
  23.             } catch (Exception e) {
  24.             }
  25.         }
  26.  
  27.         /**
  28.          * Verbindung schließen
  29.          */
  30.         public void close() {
  31.             try {
  32.                 if (con != null) {
  33.                     con.close();
  34.                 }
  35.             } catch (Exception e) {
  36.                 e.printStackTrace();
  37.             }
  38.         }
  39.        
  40.         /**
  41.          * DB-Anfrage
  42.          */
  43.         public void update(String qry) {
  44.             try {
  45.                 Statement st = con.createStatement();
  46.                 st.executeUpdate(qry);
  47.                 st.close();
  48.             } catch (Exception e) {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.  
  53.         /**
  54.          * DB-Manipulation
  55.          */
  56.         public ResultSet query(String qry) {
  57.  
  58.             ResultSet rs = null;
  59.  
  60.             try {
  61.                 Statement st = con.createStatement();
  62.                 rs = st.executeQuery(qry);
  63.             } catch (Exception e) {
  64.                 e.printStackTrace();
  65.             }
  66.             return rs;
  67.         }
  68. }
Add Comment
Please, Sign In to add comment