Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. public class MySQL implements Cloneable {
  2.     private static final String INSERT = "INSERT INTO :table VALUES(:total);";
  3.     private static final String SELECT = "SELECT :fields FROM :table WHERE :fieldname=:value;";
  4.     private static final String SELECT_ALL = "SELECT :fields FROM :table;";
  5.     private static final String UPDATE = "UPDATE :table SET :fields WHERE :fieldname=:value;";
  6.     //MySQL Credentials
  7.     private String server;
  8.     private String port;
  9.     private String database;
  10.     private String user;
  11.     private String pass;
  12.     private Connection sqlConnection;
  13.  
  14.     public MySQL(String server, String port, String database, String user, String pass) {
  15.         this.server = server;
  16.         this.port = port;
  17.         this.database = database;
  18.         this.user = user;
  19.         this.pass = pass;
  20.     }
  21.  
  22.     public boolean connect() {
  23.         System.out.println("Attempting connection to " + "jdbc:mysql://" + server + ":" + port + "/" + database);
  24.         try {
  25.             this.sqlConnection = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, user, pass);
  26.             return true;
  27.         } catch (SQLException localException) {
  28.             //System.out.println("Connection failed.");
  29.             //localException.printStackTrace();
  30.             return false;
  31.         }
  32.     }
  33.  
  34.     public Connection getConnection() {
  35.         return this.sqlConnection;
  36.     }
  37.  
  38.     public MySQLResponse executeQuery(String query, Object... sets) {
  39.         try {
  40.             if (this.sqlConnection.isClosed()) {
  41.                 this.connect();
  42.             }
  43.         } catch (SQLException localException) {
  44.         }
  45.         try {
  46.             PreparedStatement statement = this.sqlConnection.prepareStatement(query);
  47.  
  48.             int index = 1;
  49.             if (sets != null) for (Object obj : sets) {
  50.                 statement.setObject(index, obj);
  51.  
  52.                 index += 1;
  53.             }
  54.  
  55.             return new MySQLResponse(statement, statement.executeQuery());
  56.         } catch (SQLException localException) {
  57.             localException.printStackTrace();
  58.  
  59.             return null;
  60.         }
  61.     }
  62.  
  63.     public MySQLResponse execute(String query, Object... sets) {
  64.         try {
  65.             if (this.sqlConnection.isClosed()) {
  66.                 this.connect();
  67.             }
  68.         } catch (SQLException localException) {
  69.         }
  70.         try {
  71.             PreparedStatement statement = this.sqlConnection.prepareStatement(query);
  72.  
  73.             int index = 1;
  74.             if (sets != null) for (Object obj : sets) {
  75.                 statement.setObject(index, obj);
  76.  
  77.                 index += 1;
  78.             }
  79.  
  80.             statement.execute();
  81.  
  82.             return new MySQLResponse(statement);
  83.         } catch (SQLException localException) {
  84.             localException.printStackTrace();
  85.  
  86.             return null;
  87.         }
  88.     }
  89.  
  90.     public MySQLResponse executeUpdate(String query, Object... sets) {
  91.         try {
  92.             if (this.sqlConnection.isClosed()) {
  93.                 this.connect();
  94.             }
  95.         } catch (SQLException localException) {
  96.         }
  97.         try {
  98.             PreparedStatement statement = this.sqlConnection.prepareStatement(query);
  99.  
  100.             int index = 1;
  101.             if (sets != null) for (Object obj : sets) {
  102.                 statement.setObject(index, obj);
  103.  
  104.                 index += 1;
  105.             }
  106.  
  107.             statement.executeUpdate();
  108.  
  109.             return new MySQLResponse(statement);
  110.         } catch (SQLException localException) {
  111.             localException.printStackTrace();
  112.  
  113.             return null;
  114.         }
  115.     }
  116.  
  117.     public MySQLResponse exists(String table, String fieldName, String value, String fields) {
  118.         try {
  119.             if (this.sqlConnection.isClosed()) {
  120.                 this.connect();
  121.             }
  122.         } catch (SQLException localException) {
  123.         }
  124.         PreparedStatement statement = null;
  125.         try {
  126.             statement = this.sqlConnection.prepareStatement(select(fields, table, fieldName, "?"));
  127.             statement.setString(1, value);
  128.             ResultSet set = statement.executeQuery();
  129.  
  130.             return new MySQLResponse(statement, set, set.next());
  131.         } catch (SQLException localException) {
  132.             localException.printStackTrace();
  133.  
  134.             return new MySQLResponse(statement, null, false);
  135.         }
  136.     }
  137.  
  138.     public String update(String table, List<String> fields, String fieldName, String value) {
  139.         String fieldString = "";
  140.         Integer.parseInt(fieldString);
  141.         int index = 0;
  142.         for (String field : fields) {
  143.             if (index == 0) fieldString += field + "=?";
  144.             else fieldString += ", " + field + "=?";
  145.             index += 1;
  146.         }
  147.  
  148.         System.out.println(UPDATE.replace(":table", table).replace(":fields", fieldString).replace(":fieldname", fieldName).replace(":value", value));
  149.  
  150.         return UPDATE.replace(":table", table).replace(":fields", fieldString).replace(":fieldname", fieldName).replace(":value", value);
  151.     }
  152.  
  153.     public String insert(String table, int unknowns) {
  154.         String total = "";
  155.  
  156.         for (int i = 0; i < unknowns; i++) {
  157.             if (i == 0) total += "?";
  158.             else total += ", ?";
  159.         }
  160.  
  161.         System.out.println(INSERT.replace(":table", table).replace(":total", total));
  162.  
  163.         return INSERT.replace(":table", table).replace(":total", total);
  164.     }
  165.  
  166.     public String select(String fields, String table, String fieldName, String value) {
  167.         System.out.println(SELECT.replace(":fields", fields).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value));
  168.  
  169.         return SELECT.replace(":fields", fields).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value);
  170.     }
  171.     public String select(String table, String fieldName, String value, String... fields) {
  172.         StringBuilder formattedFields = new StringBuilder();
  173.         for (int x = 0; x<fields.length; x++) {
  174.             if (x != 0) {
  175.                 formattedFields.append(" ");
  176.             }
  177.             formattedFields.append(fields[x]);
  178.         }
  179.         String query = SELECT.replace(":fields", formattedFields.toString().replace(" ", ", ")).replace(":table", table).replace(":fieldname", fieldName).replace(":value", value);
  180.         System.out.println(query);
  181.         return query;
  182.     }
  183.  
  184.     public String selectAll(String table, String... fields) {
  185.         StringBuilder formattedFields = new StringBuilder();
  186.         for (int x = 0; x<fields.length; x++) {
  187.             if (x != 0) {
  188.                 formattedFields.append(" ");
  189.             }
  190.             formattedFields.append(fields[x]);
  191.         }
  192.         String query = SELECT_ALL.replace(":table", table).replace(":fields", formattedFields.toString().replace(" ", ", "));
  193.         System.out.println(query);
  194.         return query;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement