added sql consumer
This commit is contained in:
parent
77f3f48b97
commit
181704e5a7
13
src/main/java/api/SQLConsumer.java
Normal file
13
src/main/java/api/SQLConsumer.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package api;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* @author Mathias Wagner
|
||||||
|
* Created 25.12.2020
|
||||||
|
*******************************/
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SQLConsumer<T> {
|
||||||
|
void accept(T t) throws SQLException;
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package drivers;
|
package drivers;
|
||||||
|
|
||||||
|
import api.SQLConsumer;
|
||||||
import manager.ResultManager;
|
import manager.ResultManager;
|
||||||
import manager.DataBaseSelection;
|
import manager.DataBaseSelection;
|
||||||
import manager.UpdateManager;
|
import manager.UpdateManager;
|
||||||
@ -168,6 +169,20 @@ public class MySQLConnection {
|
|||||||
return new ResultManager(getResultSet(query, params), sqlLogManager);
|
return new ResultManager(getResultSet(query, params), sqlLogManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a action with a result from your server
|
||||||
|
* @param query Search query
|
||||||
|
* @param consumer consumer
|
||||||
|
* @param params Optional parameters
|
||||||
|
*/
|
||||||
|
public void getResult(String query, SQLConsumer<ResultSet> consumer, Object... params) {
|
||||||
|
try {
|
||||||
|
ResultSet resultSet = getResultSet(query, params);
|
||||||
|
consumer.accept(resultSet);
|
||||||
|
resultSet.close();
|
||||||
|
} 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
|
||||||
@ -229,7 +244,7 @@ public class MySQLConnection {
|
|||||||
con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password);
|
con = DriverManager.getConnection("jdbc:mysql://" + hostname + "/" + database + connectString, username, password);
|
||||||
sqlLogManager.sendInfo("Connection established");
|
sqlLogManager.sendInfo("Connection established");
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
if (exception.getMessage().equals("jdbc.Driver"))
|
if (exception.getMessage().contains("jdbc.Driver"))
|
||||||
sqlLogManager.sendError("MySQL driver not found! Check your dependencies or install the JDBC Mysql Driver from https://dev.mysql.com/downloads/file/?id=498587");
|
sqlLogManager.sendError("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")) sqlLogManager.sendError("No packets received from the server.");
|
else if (exception.getMessage().contains("has not received any packets")) sqlLogManager.sendError("No packets received from the server.");
|
||||||
else if (exception.getMessage().contains("denied for user")) sqlLogManager.sendError("Wrong username/password");
|
else if (exception.getMessage().contains("denied for user")) sqlLogManager.sendError("Wrong username/password");
|
||||||
|
Reference in New Issue
Block a user