Advertisement
Guest User

Untitled

a guest
May 1st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 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 + "?" +
  23.                 "autoReconnect=true", this.user, this.password);
  24.             } catch (Exception e) {
  25.             }
  26.         }
  27.  
  28.         /**
  29.          * Verbindung schließen
  30.          */
  31.         public void close() {
  32.             try {
  33.                 if (con != null) {
  34.                     con.close();
  35.                 }
  36.             } catch (Exception e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.        
  41.         /**
  42.          * DB-Anfrage
  43.          */
  44.         public void update(String qry) {
  45.             try {
  46.                 Statement st = con.createStatement();
  47.                 st.executeUpdate(qry);
  48.                 st.close();
  49.             } catch (Exception e) {
  50.                 e.printStackTrace();
  51.             }
  52.         }
  53.  
  54.         /**
  55.          * DB-Manipulation
  56.          */
  57.         public ResultSet query(String qry) {
  58.  
  59.             ResultSet rs = null;
  60.  
  61.             try {
  62.                 Statement st = con.createStatement();
  63.                 rs = st.executeQuery(qry);
  64.             } catch (Exception e) {
  65.                 e.printStackTrace();
  66.             }
  67.             return rs;
  68.         }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement