Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.steppvp.kitpvp.mysql;
- import de.steppvp.kitpvp.main.Main;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.Bukkit;
- public class MySQL {
- public static Connection con;
- public static MySQL mysql;
- public static String user = "root";
- public static String pass = "kikidiki777";
- public static String host = "127.0.0.1";
- public static String db = "minecraft";
- public static void connect() {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + db + "?autoReconnect=true", user, pass);
- Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung wurde erfolgreich hergestellt.");
- update("CREATE TABLE IF NOT EXISTS Kopfgeld (UUID varchar(64), Kopfgeld int)");
- update("CREATE TABLE IF NOT EXISTS Money (UUID varchar(64), Money int)");
- update("CREATE TABLE IF NOT EXISTS Stats (UUID varchar(64), KILLS int, DEATHS int)");
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung ist fehlgeschlagen!");
- }
- }
- public static void close() {
- try {
- if (con != null) {
- con.close();
- con = null;
- Bukkit.getConsoleSender().sendMessage("Die MySQL-Verbindung wurde getrennt.");
- }
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- public static void update(String qry) {
- try {
- Statement stmt = con.createStatement();
- stmt.executeUpdate(qry);
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- public static ResultSet Query(String query) {
- ResultSet rs = null;
- try {
- Statement stmt = con.createStatement();
- stmt.executeQuery(query);
- return stmt.getResultSet();
- } catch (Exception e) {
- System.err.println(e);
- }
- return rs;
- }
- public static String getFirstString(ResultSet rs, int l, String re, int t) {
- try {
- while (rs.next()) {
- if (rs.getString(l).equalsIgnoreCase(re)) {
- return rs.getString(t);
- }
- }
- } catch (Exception localException) {}
- return null;
- }
- public static int getFirstInt(ResultSet rs, int l, String re, int t) {
- try {
- while (rs.next()) {
- if (rs.getString(l).equalsIgnoreCase(re)) {
- return rs.getInt(t);
- }
- }
- } catch (Exception localException) {}
- return 0;
- }
- public static Connection getConnection() {
- return con;
- }
- public static boolean isConnected() {
- return con != null;
- }
- public static ResultSet getResult(String qry) {
- if (isConnected()) {
- try {
- return con.createStatement().executeQuery(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public static void closeRessources(ResultSet rs, PreparedStatement st) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException localSQLException) {}
- }
- if (st != null) {
- try {
- st.close();
- } catch (SQLException localSQLException1) {}
- }
- }
- public static void close(PreparedStatement st, ResultSet rs) {
- try {
- if (st != null) {
- st.close();
- }
- if (rs != null) {
- rs.close();
- }
- } catch (Exception localException) {}
- }
- public static void executeUpdate(String statement) {
- try {
- PreparedStatement st = con.prepareStatement(statement);
- st.executeUpdate();
- close(st, null);
- } catch (Exception e) {
- Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgefuehrt werden: " + e.getMessage());
- e.printStackTrace();
- }
- }
- public static void executeUpdate(PreparedStatement statement) {
- try {
- statement.executeUpdate();
- close(statement, null);
- } catch (Exception e) {
- Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgefuehrt werden: " + e.getMessage());
- e.printStackTrace();
- }
- }
- public static ResultSet executeQuery(String statement) {
- try {
- PreparedStatement st = con.prepareStatement(statement);
- return st.executeQuery();
- } catch (Exception e) {
- Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgefuehrt werden: " + e.getMessage());
- }
- return null;
- }
- public static ResultSet executeQuery(PreparedStatement statement) {
- try {
- return statement.executeQuery();
- } catch (Exception e) {
- Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgefuehrt werden: " + e.getMessage());
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement