Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.rizegames.bungeesystem.bungeeDB;
- import de.rizegames.bungeesystem.Core;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL
- {
- private final Core main;
- private final String host;
- private final String database;
- private final String user;
- private final String password;
- private int port;
- private Connection conn;
- public MySQL(Core main, String host, int port, String user, String password, String database)
- {
- this.port = 3306;
- this.main = main;
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- this.database = database;
- }
- public void openConnection()
- {
- try
- {
- if (!isConnectionOpen()) {
- this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
- }
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- }
- public void closeConnection()
- {
- try
- {
- if (isConnectionOpen())
- {
- this.conn.close();
- this.conn = null;
- }
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- }
- private boolean isConnectionOpen()
- {
- try
- {
- return (this.conn != null) && (this.conn.isValid(3));
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- return false;
- }
- public Connection getConnection()
- {
- return this.conn;
- }
- public boolean executeUpdate(String syntax)
- {
- try
- {
- PreparedStatement statement = this.conn.prepareStatement(syntax);
- statement.executeUpdate();
- statement.close();
- return true;
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- return false;
- }
- public ResultSet getResult(String syntax)
- {
- try
- {
- PreparedStatement statement = this.conn.prepareStatement(syntax);
- return statement.executeQuery();
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- return null;
- }
- public boolean existResult(String table, String where, Object value)
- {
- ResultSet rs = getResult("SELECT * FROM " + table + " WHERE " + where + "='" + value + "'");
- try
- {
- if (rs.next()) {
- return true;
- }
- rs.close();
- }
- catch (SQLException e)
- {
- sendException(e);
- }
- return false;
- }
- private void sendException(Exception e)
- {
- this.main.sendConsole("�cFolgender Fehler ist aufgereten:");
- this.main.sendConsole("�c" + e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement