Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.joseph.murder;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MySQL {
- private String host;
- private String database;
- private String user;
- private String password;
- private String port;
- private Connection con;
- public MySQL(String host, String port, String database, String user, String password) {
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- this.port = port;
- connect();
- if (isConnected()) {
- System.out.println("[Murder] Connected to MySQL database.");
- createTable();
- }
- if (!isConnected()) {
- System.out.println("[Murder] Could not connect to MySQL.");
- }
- }
- public boolean isConnected() {
- try {
- if (getConnection().isClosed()) {
- return false;
- }
- return true;
- } catch (Exception e) {
- }
- return false;
- }
- public void createTable() {
- try (Statement statement = getConnection().createStatement();) {
- statement.executeUpdate(
- "CREATE TABLE IF NOT EXISTS MurderData (uuid VARCHAR(64), wins INT(10), loses INT(10), deaths INT(10), kills INT(10), score INT(10));");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connect() {
- try {
- con = DriverManager.getConnection(
- "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true",
- this.user, this.password);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- public void close() {
- if (isConnected()) {
- try {
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public ResultSet query(String qry) {
- if (isConnected()) {
- ResultSet rs = null;
- try {
- Statement st = con.createStatement();
- rs = st.executeQuery(qry);
- } catch (SQLException ex) {
- connect();
- ex.printStackTrace();
- }
- return rs;
- }
- return null;
- }
- public void update(String qry) {
- if (isConnected()) {
- try {
- Statement st = con.createStatement();
- st.executeUpdate(qry);
- st.close();
- } catch (SQLException ex) {
- connect();
- ex.printStackTrace();
- }
- }
- }
- public Connection getConnection() {
- return con;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement