Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.wumptrax.sql;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL {
- private static String user;
- private static String database;
- private static String password;
- private static String port;
- private static String hostname;
- private static Connection connection = null;
- public static boolean checkTable(String name) {
- boolean exists = false;
- try {
- DatabaseMetaData meta = MySQL.getConnection().getMetaData();
- ResultSet res;
- res = meta.getTables(null, null, "", new String[] { "TABLE" });
- while (res.next()) {
- if (res.getString("TABLE_NAME").equals(name))
- exists = true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return exists;
- }
- public static void loadMySQLSettings() {
- //TODO: CONFIG
- }
- public static Connection getConnection() {
- if (isConnected())
- return connection;
- else {
- System.out.println("[MySQL] Lost MySQL Connection. Reconnecting...");
- connect();
- return connection;
- }
- }
- public static boolean isConnected() {
- try {
- return connection != null && connection.isValid(120);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void connect() {
- if (isConnected()) {
- try {
- connection.close();
- } catch (SQLException e) {
- }
- }
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database, user,
- password);
- System.out.println("[MySQL] Connected to database!");
- } catch (SQLException e) {
- System.out.println("[MySQL] Could not connect to MySQL server! Because: " + e.getMessage());
- } catch (ClassNotFoundException e) {
- System.out.println("[MySQL] JDBC Driver not found!");
- }
- }
- public static void closeConnection() {
- if (connection != null) {
- try {
- connection.close();
- connection = null;
- } catch (SQLException e) {
- System.out.println("[MySQL] Error while closing the MySQL Connection!");
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement