Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQLConnector {
- private static String user;
- private static String password;
- private static String host;
- private static String database;
- private static Integer port;
- private static Connection connection;
- @SuppressWarnings("static-access")
- public MySQLConnector(String user, String password, String host, String database, Integer port)
- {
- this.user = user;
- this.password = password;
- this.host = host;
- this.database = database;
- this.port = port;
- }
- public static void connect()
- {
- if (!isConnected()) {
- try
- {
- connection = DriverManager.getConnection("jdbc:mysql://" +
- host + ":" + port + "/" + database +
- "?autoReconnect=true", user, password);
- Bukkit.getConsoleSender().sendMessage(Main.Prefix + "§aDie Verbindung zu der MySQL Datenbank wurde erfolgreich erstellt");
- }
- catch (SQLException e)
- {
- Bukkit.getConsoleSender().sendMessage(Main.Prefix + "§cDie Verbindung zu der MySQL Datenbank konnte nicht hergerstellt werden");
- }
- }
- }
- @SuppressWarnings("static-access")
- public void close()
- {
- if (isConnected()) {
- try
- {
- this.connection.close();
- }
- catch (SQLException localSQLException) {}
- }
- }
- public static boolean isConnected()
- {
- return connection != null;
- }
- public static void update(String qry)
- {
- try
- {
- PreparedStatement preparedStatement = connection
- .prepareStatement(qry);
- preparedStatement.executeUpdate();
- }
- catch (SQLException e)
- {
- connect();
- System.err.println(e);
- }
- }
- public static ResultSet query(String qry)
- {
- ResultSet rs = null;
- try
- {
- PreparedStatement preparedStatement = connection
- .prepareStatement(qry);
- rs = preparedStatement.executeQuery();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement