Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sleiferhd.core.config;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MysqlConnection {
- private final String host;
- private final int port;
- private final String database;
- private final String username;
- private final String password;
- private Connection conn;
- public final Mysql inf;
- public MysqlConnection(String host, int port, String database, String username, String password)
- {
- this.host = host;
- this.port = port;
- this.database = database;
- this.username = username;
- this.password = password;
- this.inf = new Mysql(this);
- reconnect();
- }
- public void reconnect()
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
- } catch(Exception err) {err.printStackTrace();}
- }
- public boolean isConnected()
- {
- try {
- return conn == null ? false : !conn.isClosed();
- } catch(Exception err) {err.printStackTrace();}
- return false;
- }
- public void executeUpdate(String update)
- {
- if(!isConnected())
- reconnect();
- try {
- conn.createStatement().executeUpdate(update);
- } catch(Exception err) {err.printStackTrace();}
- }
- public ResultSet executeQuery(String query)
- {
- if(!isConnected())
- reconnect();
- try {
- return conn.createStatement().executeQuery(query);
- } catch(Exception err) {err.printStackTrace();}
- return null;
- }
- public void close()
- {
- if(isConnected())
- try {
- conn.close();
- conn = null;
- } catch(SQLException err) {err.printStackTrace();}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement