Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import org.bukkit.plugin.Plugin;
- public class MySQL {
- private final String user;
- private final String database;
- private final String password;
- private final String port;
- private final String hostname;
- private Connection connection;
- private Plugin plugin;
- public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password) {
- this.plugin = plugin;
- this.hostname = hostname;
- this.port = port;
- this.database = database;
- this.user = username;
- this.password = password;
- this.connection = null;
- }
- public Connection openConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
- } catch (SQLException e) {
- plugin.getLogger().log(Level.SEVERE, "Verbindung unterbrochen, weil: " + e.getMessage());
- } catch (ClassNotFoundException e) {
- plugin.getLogger().log(Level.SEVERE, "JDBC Driver wurde nicht gefunden!");
- }
- return connection;
- }
- public boolean checkConnection() {
- return connection != null;
- }
- public Connection getConnection() {
- return connection;
- }
- public void closeConnection() {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- plugin.getLogger().log(Level.SEVERE, "Fehler die MySQL verbindung wurde unterbrochen!");
- e.printStackTrace();
- }
- }
- }
- public ResultSet querySQL(String query) {
- Connection c = null;
- if (checkConnection()) {
- c = getConnection();
- } else {
- c = openConnection();
- }
- Statement s = null;
- try {
- s = c.createStatement();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- ResultSet ret = null;
- try {
- ret = s.executeQuery(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- closeConnection();
- return ret;
- }
- public void updateSQL(String update) {
- Connection c = null;
- if (checkConnection()) {
- c = getConnection();
- } else {
- c = openConnection();
- }
- Statement s = null;
- try {
- s = c.createStatement();
- s.executeUpdate(update);
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- closeConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement