Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final String MYSQL_SERVER = "127.0.0.1";
- private static Connection con;
- private static String hostname = MYSQL_SERVER, port = "3306", database = "osrs", user = "root",
- password = "wachtwoord";
- public static final void initialize(String host, String port, String database, String user, String password) {
- MySQL.hostname = host;
- MySQL.port = port;
- MySQL.database = database;
- MySQL.user = user;
- MySQL.password = password;
- }
- public static synchronized void openConnection() throws SQLException, ClassNotFoundException {
- closeConnection();// make sure the connection is not already open
- Class.forName("com.mysql.cj.jdbc.Driver");
- System.out.println("Opening mysql con");
- DriverManager.setLoginTimeout(15000);
- con = DriverManager.getConnection(
- "jdbc:mysql://" + MySQL.hostname + ":" + MySQL.port + "/" + MySQL.database + "?allowMultiQueries=true",
- MySQL.user, MySQL.password);
- con.prepareStatement("USE " + database).execute();
- }
- public static synchronized void closeConnection() {
- try {
- if (con != null && !con.isClosed())
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static synchronized void checkConnection() {
- try {
- if (con == null || !con.isValid(0))
- openConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement