Advertisement
Guest User

Untitled

a guest
Aug 12th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. public class MySQL {
  2.    
  3.     private MySQLCreditials credetails;
  4.     private Connection connection;
  5.    
  6.     public MySQL(MySQLCreditials creditails) {
  7.         this.credetails = creditails;
  8.     }
  9.    
  10.     public void connect() throws Exception {
  11.         Class.forName("com.mysql.jdbc.Driver");
  12.         this.connection = DriverManager.getConnection("jdbc:mysql://" + credetails.getHost() + ":" + credetails.getPort() + "/" + credetails.getDatabase() + "?autoReconnect=" + credetails.isAutoReconnect().toString() + "", credetails.getUser(), credetails.getPassword());
  13.     }
  14.    
  15.     public void close() throws SQLException {
  16.         this.connection.close();
  17.     }
  18.    
  19.     public void update(String qry) {
  20.         try {
  21.             Statement st = (Statement) connection.createStatement();
  22.             st.executeUpdate(qry);
  23.             st.close();
  24.         } catch (SQLException e) {
  25.             System.err.println(e);
  26.         }
  27.     }
  28.  
  29.     public ResultSet query(String qry) {
  30.         ResultSet rs = null;
  31.         try {
  32.             Statement st = (Statement) connection.createStatement();
  33.             rs = st.executeQuery(qry);
  34.         } catch (SQLException e) {
  35.             System.err.println(e);
  36.         }
  37.         return rs;
  38.     }
  39.    
  40.     public String getString(String table, String row, String optionkey, String optionvalue) {
  41.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "' LIMIT 1");
  42.         try {
  43.             if (rs.next() && rs.getString(row) != null) {
  44.                 return rs.getString(row);
  45.             }
  46.         } catch (SQLException e) {}
  47.         return null;
  48.     }
  49.    
  50.     public String getString(String table, String row) {
  51.         ResultSet rs = query("SELECT " + row + " FROM " + table + " LIMIT 1");
  52.         try {
  53.             if (rs.next() && rs.getString(row) != null) {
  54.                 return rs.getString(row);
  55.             }
  56.         } catch (SQLException e) {}
  57.         return null;
  58.     }
  59.    
  60.     public void setString(String table, String row, String value, String optionkey, String optionvalue) {
  61.         update("UPDATE " + table + " SET " + row + " = '" + value + "' WHERE " + optionkey + " = '" + optionvalue + "'");
  62.     }
  63.    
  64.     public void setString(String table, String row, String value) {
  65.         update("UPDATE " + table + " SET " + row + " = '" + value + "' WHERE 1");
  66.     }
  67.    
  68.     public void addInteger(String table, String row, String optionkey, String optionvalue) {
  69.         update("UPDATE " + table + " SET " + row + " = " + row + " + 1 WHERE " + optionkey + " = '" + optionvalue + "'");
  70.     }
  71.    
  72.     public ArrayList<String> getList(String table, String row) {
  73.         ArrayList<String> list = new ArrayList<String>();
  74.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE 1");
  75.         try {
  76.             while(rs.next()) {
  77.                 list.add(rs.getString(row));
  78.             }
  79.         } catch (SQLException e) {}
  80.         return list;
  81.     }
  82.    
  83.     public ArrayList<String> getListSelectAll(String table) {
  84.         ArrayList<String> list = new ArrayList<String>();
  85.         ResultSet rs = query("SELECT " + "*" + " FROM " + table + " WHERE 1");
  86.         try {
  87.             while(rs.next()) {
  88.                 list.add(rs.getString(1));
  89.             }
  90.         } catch (SQLException e) {}
  91.         return list;
  92.     }
  93.    
  94.     public ArrayList<String> getList(String table, String row, String optionkey, String optionvalue) {
  95.         ArrayList<String> list = new ArrayList<String>();
  96.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "'");
  97.         try {
  98.             while(rs.next()) {
  99.                 list.add(rs.getString(row));
  100.             }
  101.         } catch (SQLException e) {}
  102.         return list;
  103.     }
  104.    
  105.     public ArrayList<String> getListSelectAll(String table, String optionkey, String optionvalue) {
  106.         ArrayList<String> list = new ArrayList<String>();
  107.         ResultSet rs = query("SELECT " + "*" + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "'");
  108.         try {
  109.             while(rs.next()) {
  110.                 list.add(rs.getString(1));
  111.             }
  112.         } catch (SQLException e) {}
  113.         return list;
  114.     }
  115.    
  116.     public ArrayList<String> getList(String table, String row, String optionkey, String optionvalue, Integer limit) {
  117.         ArrayList<String> list = new ArrayList<String>();
  118.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "' LIMIT " + limit);
  119.         try {
  120.             while(rs.next()) {
  121.                 list.add(rs.getString(row));
  122.             }
  123.         } catch (SQLException e) {}
  124.         return list;
  125.     }
  126.    
  127.     public ArrayList<String> getList(String table, String row, String optionkey, String optionvalue, String order, OrderMode mode) {
  128.         ArrayList<String> list = new ArrayList<String>();
  129.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "' ORDER BY " + order + " " + mode.name());
  130.         try {
  131.             while(rs.next()) {
  132.                 list.add(rs.getString(row));
  133.             }
  134.         } catch (SQLException e) {}
  135.         return list;
  136.     }
  137.    
  138.     public ArrayList<String> getList(String table, String row, String optionkey, String optionvalue, Integer limit, String order, OrderMode mode) {
  139.         ArrayList<String> list = new ArrayList<String>();
  140.         ResultSet rs = query("SELECT " + row + " FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "' ORDER BY " + order + " " + mode.name() + " LIMIT " + limit);
  141.         try {
  142.             while(rs.next()) {
  143.                 list.add(rs.getString(row));
  144.             }
  145.         } catch (SQLException e) {}
  146.         return list;
  147.     }
  148.    
  149.     public HashMap<String, String> getRow(String table, String optionkey, String optionvalue) {
  150.         HashMap<String, String> map = new HashMap<String, String>();
  151.         ResultSet rs = query("SELECT * FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "'");
  152.         try {
  153.             ResultSetMetaData meta = rs.getMetaData();
  154.             rs.next();
  155.             for (int i = 1; i <= meta.getColumnCount(); i++) {
  156.                 String key = meta.getColumnName(i);
  157.                 String value = rs.getString(key);
  158.                 map.put(key, value);
  159.             }
  160.         } catch (Exception e) {
  161.             e.printStackTrace();
  162.         }
  163.         return map;
  164.     }
  165.    
  166.     public HashMap<String, String> getRow(String table) {
  167.         HashMap<String, String> map = new HashMap<String, String>();
  168.         ResultSet rs = query("SELECT * FROM " + table + " WHERE 1");
  169.         try {
  170.             ResultSetMetaData meta = rs.getMetaData();
  171.             rs.next();
  172.             for (int i = 1; i <= meta.getColumnCount(); i++) {
  173.                 String key = meta.getColumnName(i);
  174.                 String value = rs.getString(key);
  175.                 map.put(key, value);
  176.             }
  177.         } catch (Exception e) {
  178.             e.printStackTrace();
  179.         }
  180.         return map;
  181.     }
  182.    
  183.     public void insert(String table, String keys, String values) {
  184.         update("INSERT INTO " + table + " (" + keys + ") VALUES (" + values + ")");
  185.     }
  186.    
  187.     public void delete(String table, String optionkey, String optionvalue) {
  188.         update("DELETE FROM " + table + " WHERE " + optionkey + " = '" + optionvalue + "'");
  189.     }
  190.    
  191.     public void createTable(String name, String struckture) {
  192.         update("CREATE TABLE " + name + " (" + struckture + ")");
  193.     }
  194.    
  195.     public void createTableIfNotExists(String name, String struckture) {
  196.         update("CREATE TABLE IF NOT EXISTS " + name + " (" + struckture + ")");
  197.     }
  198.    
  199.     public void dropTable(String table) {
  200.         update("DROP TABLE " + table);
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement