Cleaned up the MySQLConnection class

This commit is contained in:
mathias 2021-08-19 12:41:15 +02:00
parent 47c8a1391d
commit 2d220c0211
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A

View File

@ -30,6 +30,7 @@ public class MySQLConnection {
/** /**
* Basic constructor for the connection * Basic constructor for the connection
*
* @param hostname MySQL server hostname * @param hostname MySQL server hostname
* @param username MySQL server username * @param username MySQL server username
* @param password MySQL server password * @param password MySQL server password
@ -44,6 +45,7 @@ public class MySQLConnection {
/** /**
* Advanced constructor for the connection * Advanced constructor for the connection
*
* @param hostname MySQL server hostname * @param hostname MySQL server hostname
* @param username MySQL server username * @param username MySQL server username
* @param password MySQL server password * @param password MySQL server password
@ -60,15 +62,20 @@ public class MySQLConnection {
/** /**
* Get a result from your server * Get a result from your server
*
* @param query Search query * @param query Search query
* @param params Optional parameters * @param params Optional parameters
* @return ResultSet * @return ResultSet
*/ */
public ResultSet getResultSet(String query, Object... params) { public ResultSet getResultSet(String query, Object... params) {
query = (tablePrefix.isEmpty()) ? query : query.replace((tablePrefixVariable.isEmpty()) ? "$tp" : tablePrefixVariable, tablePrefix); query = (tablePrefix.isEmpty()) ? query : query.replace((tablePrefixVariable.isEmpty()) ? "$tp" : tablePrefixVariable, tablePrefix);
try { int start = 1; try {
int start = 1;
PreparedStatement ps = con.prepareStatement(query); PreparedStatement ps = con.prepareStatement(query);
for (Object current : params) { ps.setObject(start, current);start++; } for (Object current : params) {
ps.setObject(start, current);
start++;
}
return ps.executeQuery(); return ps.executeQuery();
} catch (Exception err) { } catch (Exception err) {
LOG.error(err.getMessage()); LOG.error(err.getMessage());
@ -78,6 +85,7 @@ public class MySQLConnection {
/** /**
* Get a result from your server (get the Manager) * Get a result from your server (get the Manager)
*
* @param query Search query * @param query Search query
* @param params Optional parameters * @param params Optional parameters
* @return ResultManager * @return ResultManager
@ -88,6 +96,7 @@ public class MySQLConnection {
/** /**
* Run a action with a result from your server * Run a action with a result from your server
*
* @param query Search query * @param query Search query
* @param consumer consumer * @param consumer consumer
* @param params Optional parameters * @param params Optional parameters
@ -97,27 +106,36 @@ public class MySQLConnection {
ResultSet resultSet = getResultSet(query, params); ResultSet resultSet = getResultSet(query, params);
consumer.accept(resultSet); consumer.accept(resultSet);
resultSet.close(); resultSet.close();
} catch (Exception ignored) {} } catch (Exception ignored) {
}
} }
/** /**
* Update something on your server by query * Update something on your server by query
*
* @param query Update query * @param query Update query
* @param params Optional parameters * @param params Optional parameters
* @return this class * @return this class
*/ */
public MySQLConnection update(String query, Object... params) { public MySQLConnection update(String query, Object... params) {
query = (tablePrefix.isEmpty()) ? query : query.replace((tablePrefixVariable.isEmpty()) ? "$tp" : tablePrefixVariable, tablePrefix); query = (tablePrefix.isEmpty()) ? query : query.replace((tablePrefixVariable.isEmpty()) ? "$tp" : tablePrefixVariable, tablePrefix);
try { int start = 1; try {
int start = 1;
PreparedStatement ps = con.prepareStatement(query); PreparedStatement ps = con.prepareStatement(query);
for (Object current : params) { ps.setObject(start, current);start++; } for (Object current : params) {
ps.setObject(start, current);
start++;
}
ps.executeUpdate(); ps.executeUpdate();
} catch (Exception err) { err.printStackTrace(); } } catch (Exception err) {
err.printStackTrace();
}
return this; return this;
} }
/** /**
* Get the update de.gnmyt.SQLToolkit.manager for easier updating * Get the update de.gnmyt.SQLToolkit.manager for easier updating
*
* @return Update de.gnmyt.SQLToolkit.manager * @return Update de.gnmyt.SQLToolkit.manager
*/ */
public UpdateManager update() { public UpdateManager update() {
@ -126,6 +144,7 @@ public class MySQLConnection {
/** /**
* Get the update de.gnmyt.SQLToolkit.manager for easier updating (pre filled table) * Get the update de.gnmyt.SQLToolkit.manager for easier updating (pre filled table)
*
* @param tableName The name of the table * @param tableName The name of the table
* @return Update de.gnmyt.SQLToolkit.manager * @return Update de.gnmyt.SQLToolkit.manager
*/ */
@ -135,6 +154,7 @@ public class MySQLConnection {
/** /**
* Get the Database Selection for easier selection of tables (pre filled table) * Get the Database Selection for easier selection of tables (pre filled table)
*
* @param tableName The name of the table * @param tableName The name of the table
* @return DatabaseSelection * @return DatabaseSelection
*/ */
@ -144,6 +164,7 @@ public class MySQLConnection {
/** /**
* Get the Database Selection for easier selection of tables * Get the Database Selection for easier selection of tables
*
* @return DatabaseSelection * @return DatabaseSelection
*/ */
public DataBaseSelection select() { public DataBaseSelection select() {
@ -152,6 +173,7 @@ public class MySQLConnection {
/** /**
* Get the InsertManager for easier inserting to a table * Get the InsertManager for easier inserting to a table
*
* @return InsertManager * @return InsertManager
*/ */
public InsertManager insert() { public InsertManager insert() {
@ -160,6 +182,7 @@ public class MySQLConnection {
/** /**
* Get the InsertManager for easier inserting to a table * Get the InsertManager for easier inserting to a table
*
* @param tableName The name of the table you want to insert a object * @param tableName The name of the table you want to insert a object
* @return InsertManager * @return InsertManager
*/ */
@ -169,10 +192,12 @@ public class MySQLConnection {
/** /**
* Connect with your MySQL server * Connect with your MySQL server
*
* @return this class * @return this class
*/ */
public MySQLConnection connect() { public MySQLConnection connect() {
if (!isConnected()) { try { if (!isConnected()) {
try {
LOG.info("Connecting to " + hostname + " with user " + username + " to database " + database); LOG.info("Connecting to " + hostname + " with user " + username + " to database " + database);
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password); con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password);
@ -182,7 +207,8 @@ public class MySQLConnection {
LOG.error("MySQL driver not found! Check your dependencies or install the JDBC Mysql Driver from https://dev.mysql.com/downloads/file/?id=498587"); LOG.error("MySQL driver not found! Check your dependencies or install the JDBC Mysql Driver from https://dev.mysql.com/downloads/file/?id=498587");
else if (exception.getMessage().contains("has not received any packets")) else if (exception.getMessage().contains("has not received any packets"))
LOG.error("No packets received from the server."); LOG.error("No packets received from the server.");
else if (exception.getMessage().contains("denied for user")) LOG.error("Wrong username/password"); else if (exception.getMessage().contains("denied for user"))
LOG.error("Wrong username/password");
else LOG.error(exception.getMessage()); else LOG.error(exception.getMessage());
} }
} else LOG.warn("Already connected"); } else LOG.warn("Already connected");
@ -191,6 +217,7 @@ public class MySQLConnection {
/** /**
* Disconnect from your MySQL Server * Disconnect from your MySQL Server
*
* @return this class * @return this class
*/ */
public MySQLConnection disconnect() { public MySQLConnection disconnect() {
@ -199,13 +226,16 @@ public class MySQLConnection {
con.close(); con.close();
con = null; con = null;
LOG.info("Connection closed"); LOG.info("Connection closed");
} catch (Exception err) { LOG.error(err.getMessage()); } } catch (Exception err) {
LOG.error(err.getMessage());
}
} else LOG.warn("Not connected. Please connect first"); } else LOG.warn("Not connected. Please connect first");
return this; return this;
} }
/** /**
* Update the Connection String * Update the Connection String
*
* @param loginParams New login parameters * @param loginParams New login parameters
* @return this class * @return this class
*/ */
@ -216,6 +246,7 @@ public class MySQLConnection {
/** /**
* Set the Connection String * Set the Connection String
*
* @param tablePrefixVariable New table prefix variable * @param tablePrefixVariable New table prefix variable
* @return this class * @return this class
*/ */
@ -224,16 +255,6 @@ public class MySQLConnection {
return this; return this;
} }
/**
* Set the current table Prefix
* @param tablePrefix New table prefix
* @return this class
*/
public MySQLConnection setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
return this;
}
/** /**
* Get the current table Prefix * Get the current table Prefix
* @return the prefix * @return the prefix
@ -242,15 +263,29 @@ public class MySQLConnection {
return tablePrefix; return tablePrefix;
} }
/**
* Set the current table Prefix
*
* @param tablePrefix New table prefix
* @return this class
*/
public MySQLConnection setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
return this;
}
/** /**
* Get the current jdbc connection string for mysql * Get the current jdbc connection string for mysql
*
* @param loginParams login parameters * @param loginParams login parameters
* @return the string * @return the string
*/ */
private String getConnectionString(LoginParam[] loginParams) { private String getConnectionString(LoginParam[] loginParams) {
boolean used = false;StringBuilder currentString = new StringBuilder(); boolean used = false;
StringBuilder currentString = new StringBuilder();
for (LoginParam param : loginParams) { for (LoginParam param : loginParams) {
String currentChar = (used) ? "&" : "?";used = true; String currentChar = (used) ? "&" : "?";
used = true;
currentString.append(currentChar).append(param.getValue()); currentString.append(currentChar).append(param.getValue());
} }
return currentString.toString(); return currentString.toString();
@ -258,6 +293,7 @@ public class MySQLConnection {
/** /**
* Check if you are connected * Check if you are connected
*
* @return status of the connection * @return status of the connection
*/ */
public boolean isConnected() { public boolean isConnected() {