Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL implements Cloneable {
- private static final String INSERT = "INSERT INTO :table VALUES(:total);";
- private static final String SELECT = "SELECT :fields FROM :table WHERE :fieldname=:value;";
- private static final String SELECT_ALL = "SELECT :fields FROM :table;";
- private static final String UPDATE = "UPDATE :table SET :fields WHERE :fieldname=:value;";
- //MySQL Credentials
- private String server;
- private String port;
- private String database;
- private String user;
- private String pass;
- private Connection sqlConnection;
- public MySQL(String server, String port, String database, String user, String pass) {
- this.server = server;
- this.port = port;
- this.database = database;
- this.user = user;
- this.pass = pass;
- }
- public boolean connect() {
- System.out.println("Attempting connection to " + "jdbc:mysql://" + server + ":" + port + "/" + database);
- try {
- this.sqlConnection = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, user, pass);
- return true;
- } catch (SQLException localException) {
- //System.out.println("Connection failed.");
- //localException.printStackTrace();
- return false;
- }
- }
- public Connection getConnection() {
- return this.sqlConnection;
- }
- public MySQLResponse executeQuery(String query, Object... sets) {
- try {
- if (this.sqlConnection.isClosed()) {
- this.connect();
- }
- } catch (SQLException localException) {
- }
- try {
- PreparedStatement statement = this.sqlConnection.prepareStatement(query);
- int index = 1;
- if (sets != null) for (Object obj : sets) {
- statement.setObject(index, obj);
- index += 1;
- }
- return new MySQLResponse(statement, statement.executeQuery());
- } catch (SQLException localException) {
- localException.printStackTrace();
- return null;
- }
- }
- public MySQLResponse execute(String query, Object... sets) {
- try {
- if (this.sqlConnection.isClosed()) {
- this.connect();
- }
- } catch (SQLException localException) {
- }
- try {
- PreparedStatement statement = this.sqlConnection.prepareStatement(query);
- int index = 1;
- if (sets != null) for (Object obj : sets) {
- statement.setObject(index, obj);
- index += 1;
- }
- statement.execute();
- return new MySQLResponse(statement);
- } catch (SQLException localException) {
- localException.printStackTrace();
- return null;
- }
- }
- public MySQLResponse executeUpdate(String query, Object... sets) {
- try {
- if (this.sqlConnection.isClosed()) {
- this.connect();
- }
- } catch (SQLException localException) {
- }
- try {
- PreparedStatement statement = this.sqlConnection.prepareStatement(query);
- int index = 1;
- if (sets != null) for (Object obj : sets) {
- statement.setObject(index, obj);
- index += 1;
- }
- statement.executeUpdate();
- return new MySQLResponse(statement);
- } catch (SQLException localException) {
- localException.printStackTrace();
- return null;
- }
- }
- public MySQLResponse exists(String table, String fieldName, String value, String fields) {
- try {
- if (this.sqlConnection.isClosed()) {
- this.connect();
- }
- } catch (SQLException localException) {
- }
- PreparedStatement statement = null;
- try {
- statement = this.sqlConnection.prepareStatement(select(fields, table, fieldName, "?"));
- statement.setString(1, value);
- ResultSet set = statement.executeQuery();
- return new MySQLResponse(statement, set, set.next());
- } catch (SQLException localException) {
- localException.printStackTrace();
- return new MySQLResponse(statement, null, false);
- }
- }
- public String update(String table, List<String> fields, String fieldName, String value) {
- String fieldString = "";
- Integer.parseInt(fieldString);
- int index = 0;
- for (String field : fields) {
- if (index == 0) fieldString += field + "=?";
- else fieldString += ", " + field + "=?";
- index += 1;
- }
- System.out.println(UPDATE.replace(":table", table).replace(":fields", fieldString).replace(":fieldname", fieldName).replace(":value", value));
- return UPDATE.replace(":table", table).replace(":fields", fieldString).replace(":fieldname", fieldName).replace(":value", value);
- }
- public String insert(String table, int unknowns) {
- String total = "";
- for (int i = 0; i < unknowns; i++) {
- if (i == 0) total += "?";
- else total += ", ?";
- }
- System.out.println(INSERT.replace(":table", table).replace(":total", total));
- return INSERT.replace(":table", table).replace(":total", total);
- }
- public String select(String fields, String table, String fieldName, String value) {
- System.out.println(SELECT.replace(":fields", fields).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value));
- return SELECT.replace(":fields", fields).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value);
- }
- public String select(String table, String fieldName, String value, String... fields) {
- StringBuilder formattedFields = new StringBuilder();
- for (int x = 0; x<fields.length; x++) {
- if (x != 0) {
- formattedFields.append(" ");
- }
- formattedFields.append(fields[x]);
- }
- String query = SELECT.replace(":fields", formattedFields.toString().replace(" ", ", ")).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value);
- System.out.println(query);
- return query;
- }
- public String selectAll(String table, String... fields) {
- StringBuilder formattedFields = new StringBuilder();
- for (int x = 0; x<fields.length; x++) {
- if (x != 0) {
- formattedFields.append(" ");
- }
- formattedFields.append(fields[x]);
- }
- String query = SELECT_ALL.replace(":table", table).replace(":fields", formattedFields.toString().replace(" ", ", "));
- System.out.println(query);
- return query;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement