Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1.     public static final String MYSQL_SERVER = "127.0.0.1";
  2.  
  3.     private static Connection con;
  4.  
  5.     private static String hostname = MYSQL_SERVER, port = "3306", database = "osrs", user = "root",
  6.             password = "wachtwoord";
  7.  
  8.     public static final void initialize(String host, String port, String database, String user, String password) {
  9.         MySQL.hostname = host;
  10.         MySQL.port = port;
  11.         MySQL.database = database;
  12.         MySQL.user = user;
  13.         MySQL.password = password;
  14.     }
  15.  
  16.     public static synchronized void openConnection() throws SQLException, ClassNotFoundException {
  17.         closeConnection();// make sure the connection is not already open
  18.         Class.forName("com.mysql.cj.jdbc.Driver");
  19.         System.out.println("Opening mysql con");
  20.         DriverManager.setLoginTimeout(15000);
  21.         con = DriverManager.getConnection(
  22.                 "jdbc:mysql://" + MySQL.hostname + ":" + MySQL.port + "/" + MySQL.database + "?allowMultiQueries=true",
  23.                 MySQL.user, MySQL.password);
  24.         con.prepareStatement("USE " + database).execute();
  25.     }
  26.  
  27.     public static synchronized void closeConnection() {
  28.         try {
  29.             if (con != null && !con.isClosed())
  30.                 con.close();
  31.         } catch (SQLException e) {
  32.             e.printStackTrace();
  33.         }
  34.     }
  35.  
  36.     public static synchronized void checkConnection() {
  37.         try {
  38.             if (con == null || !con.isValid(0))
  39.                 openConnection();
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement