Advertisement
Guest User

MySQL API

a guest
Jul 18th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. public class MySQL {
  2.     public static String user = "user";
  3.     public static String pass = "password";
  4.     public static String host = "localhost";
  5.     public static String db = "db";
  6.     public static int port = 3306;
  7.     public static Connection con;
  8.  
  9.     public static void connect() {
  10.         try {
  11.             Class.forName("com.mysql.jdbc.Driver");
  12.             con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db + "?autoReconnect=true", user, pass);
  13.             System.out.println("[NexusCore]MySQL > Verbindung hergestellt!");
  14.         }
  15.         catch (ClassNotFoundException | SQLException e) {
  16.             System.out.println("[NexusCore]MySQL > Konnte keine Verbindung aufbauen!");
  17.         }
  18.     }
  19.  
  20.     public static void close() {
  21.         if (con != null) {
  22.             try {
  23.                 con.close();
  24.                 System.out.println("[NexusCore]MySQL > Verbindung geschlossen!");
  25.             }
  26.             catch (SQLException e) {
  27.                 e.printStackTrace();
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static void update(String qry) {
  33.         if (con != null) {
  34.             try {
  35.                 Statement stmt = con.createStatement();
  36.                 stmt.executeUpdate(qry);
  37.             }
  38.             catch (SQLException e) {
  39.                 e.printStackTrace();
  40.             }
  41.         } else {
  42.             System.out.println("[NexusCore]MySQL > Es besteht keine Verbindung!");
  43.         }
  44.     }
  45.  
  46.     public static ResultSet query(String qry) {
  47.         ResultSet rs = null;
  48.         try {
  49.             Statement stmt = con.createStatement();
  50.             rs = stmt.executeQuery(qry);
  51.         }
  52.         catch (SQLException e) {
  53.             e.printStackTrace();
  54.         }
  55.         return rs;
  56.     }
  57.  
  58.     public static void reconnect() {
  59.         MySQL.close();
  60.         MySQL.connect();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement