Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQLManager
- {
- private Connection connection = null;
- private final String strHost;
- private final int intPort;
- private final String strDatabase;
- private final String strUsername;
- private final String strPassword;
- private int retries = 0;
- private String lastError = "";
- public MySQLManager(final String strHost, final int intPort, final String strDatabase, final String strUsername, final String strPassword)
- {
- this.strHost = strHost;
- this.intPort = intPort;
- this.strDatabase = strDatabase;
- this.strUsername = strUsername;
- this.strPassword = strPassword;
- getConnection();
- }
- public Connection getConnection()
- {
- if (connection == null)
- {
- try
- {
- connection = DriverManager
- .getConnection("jdbc:mysql://" + strHost + ":" + intPort + "/" + strDatabase + "?user=" + strUsername + "&password=" + strPassword + "&autoreconnect=true");
- retries = 0;
- System.out.println("Successfully connected to the database.");
- }
- catch (final Exception e)
- {
- connection = null;
- System.err.println("Unable to connect to the " + strDatabase + " database with the user " + strUsername + " on " + strHost + " with port " + intPort + ": "
- + e.getMessage());
- System.err.println("Please check the host, port, database, username and password in the config.yml of the plugin and reload the plugin!");
- e.printStackTrace();
- }
- }
- return connection;
- }
- public String escapeString(final String strSQL)
- {
- if (strSQL == null)
- {
- return null;
- }
- return strSQL.replace("'", "").replace("´", "").replace("`", "").replace(";", "").replace("#", "").replace("--", "")
- .replace("/*", "").replace("*/", "").replace("'", "\\'");
- }
- public boolean update(final String strSQL)
- {
- Statement st = null;
- try
- {
- st = getConnection().createStatement();
- st.executeUpdate(strSQL);
- return true;
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL update '" + strSQL + "': " + e.getMessage());
- if (retries <= 3 && !lastError.equalsIgnoreCase(e.getMessage()))
- {
- System.err.println("Reconnecting...");
- connection = null;
- retries++;
- lastError = e.getMessage();
- getConnection();
- System.out.println("Retry...");
- update(strSQL);
- }
- }
- finally
- {
- try
- {
- st.close();
- }
- catch (final Exception e)
- {
- }
- }
- return false;
- }
- public ResultSet query(final String strSQL)
- {
- ResultSet rs = null;
- try
- {
- final Statement st = getConnection().createStatement();
- rs = st.executeQuery(strSQL);
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- if (retries <= 3 && !lastError.equalsIgnoreCase(e.getMessage()))
- {
- System.err.println("Reconnecting...");
- connection = null;
- retries++;
- lastError = e.getMessage();
- getConnection();
- System.out.println("Retry...");
- return query(strSQL);
- }
- }
- return rs;
- }
- public boolean hasStats(final String strTable, final String strType, String strKey, final String strValue)
- {
- strKey = escapeString(strKey);
- final String strSQL = "SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'";
- final ResultSet rstStats = query(strSQL);
- try
- {
- if (rstStats != null && rstStats.next())
- {
- return true;
- }
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- if (retries <= 3 && !lastError.equalsIgnoreCase(e.getMessage()))
- {
- System.err.println("Reconnecting...");
- connection = null;
- retries++;
- lastError = e.getMessage();
- getConnection();
- System.out.println("Retry...");
- return hasStats(strTable, strType, strKey, strValue);
- }
- }
- finally
- {
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- }
- return false;
- }
- public boolean hasStats(final String strTable, final String strType, String strKey1, String strValue1, String strKey2,
- String strValue2)
- {
- strKey1 = escapeString(strKey1);
- strKey2 = escapeString(strKey2);
- strValue1 = escapeString(strValue1);
- strValue2 = escapeString(strValue2);
- final String strSQL = "SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey1 + "='" + strValue1 + "' AND " + strKey2 + "='" + strValue2 + "'";
- final ResultSet rstStats = query(strSQL);
- try
- {
- if (rstStats != null && rstStats.next())
- {
- return true;
- }
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- if (retries <= 3 && !lastError.equalsIgnoreCase(e.getMessage()))
- {
- System.err.println("Reconnecting...");
- connection = null;
- retries++;
- lastError = e.getMessage();
- getConnection();
- System.out.println("Retry...");
- return hasStats(strTable, strType, strKey1, strValue1, strKey2, strValue2);
- }
- }
- finally
- {
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- }
- return false;
- }
- public int getStats(final String strTable, final String strType, String strKey, String strValue)
- {
- strKey = escapeString(strKey);
- strValue = escapeString(strValue);
- final String strSQL = "SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'";
- int i = 0;
- final ResultSet rstStats = query(strSQL);
- try
- {
- if (rstStats.next())
- {
- i = rstStats.getInt(1);
- }
- rstStats.close();
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- e.printStackTrace();
- }
- finally
- {
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- }
- return i;
- }
- public ResultSet getMultiStats(final String strTable, final String strType, final String strKey, final String strValue)
- {
- return query("SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'");
- }
- public String getString(final String strTable, final String strType, String strKey, final String strValue)
- {
- strKey = escapeString(strKey);
- final String strSQL = "SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'";
- String i = "";
- final ResultSet rstStats = query(strSQL);
- try
- {
- if (rstStats.next())
- {
- i = rstStats.getString(1);
- }
- rstStats.close();
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- e.printStackTrace();
- }
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- return i;
- }
- public int getInt(final String strTable, final String strType, String strKey, final String strValue)
- {
- strKey = escapeString(strKey);
- final String strSQL = "SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'";
- int i = 0;
- final ResultSet rstStats = query(strSQL);
- try
- {
- if (rstStats.next())
- {
- i = rstStats.getInt(1);
- }
- rstStats.close();
- }
- catch (final Exception e)
- {
- System.err.println("Unable to perform MySQL query '" + strSQL + "': " + e.getMessage());
- e.printStackTrace();
- }
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- return i;
- }
- public void setStats(final String strTable, final String strType, String strKey, final String strValue, Object objStats)
- {
- strKey = escapeString(strKey);
- objStats = escapeString(objStats.toString());
- update("UPDATE " + strTable + " SET " + strType + "='" + objStats + "' WHERE " + strKey + "='" + strValue + "'");
- }
- public void addStats(final String strTable, final String strType, String strKey, String strValue, final int intAmount)
- {
- strKey = escapeString(strKey);
- strValue = escapeString(strValue);
- int i = 0;
- final ResultSet rstStats = query("SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'");
- try
- {
- if (rstStats.next())
- {
- i = rstStats.getInt(1);
- i += intAmount;
- update("UPDATE " + strTable + " SET " + strType + "='" + i + "' WHERE " + strKey + "='" + strValue + "'");
- }
- }
- catch (final Exception e)
- {
- System.err.println("Unable to add stats (" + strType + " " + intAmount + ") from table " + strTable + " where " + strKey + "=" + strValue);
- e.printStackTrace();
- }
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- }
- public void removeStats(final String strTable, final String strType, String strKey, final String strValue, final int intAmount)
- {
- strKey = escapeString(strKey);
- int i = 0;
- final ResultSet rstStats = query("SELECT `" + strType + "` FROM `" + strTable + "` WHERE " + strKey + "='" + strValue + "'");
- try
- {
- if (rstStats.next())
- {
- i = rstStats.getInt(1);
- i -= intAmount;
- update("UPDATE " + strTable + " SET " + strType + "='" + i + "' WHERE " + strKey + "='" + strValue + "'");
- }
- }
- catch (final Exception e)
- {
- System.err.println("Unable to remove stats (" + strType + " " + intAmount + ") from table " + strTable + " where " + strKey + "=" + strValue);
- e.printStackTrace();
- }
- try
- {
- rstStats.close();
- }
- catch (final SQLException e)
- {
- }
- }
- public ResultSet getResultSet(final String strTable, final String strColumn)
- {
- return query("SELECT " + strColumn + " FROM " + strTable);
- }
- public void destroy()
- {
- try
- {
- if (connection != null && !connection.isClosed())
- {
- connection.close();
- }
- }
- catch (final Exception e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement