Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.96 KB | None | 0 0
  1. package core.database;
  2.  
  3. import java.sql.*;
  4.  
  5. public class MySQL {
  6.     private static Class driverClass;
  7.     private static Connection connection;
  8.     private static Statement statement;
  9.    
  10.     public static boolean Open(String host, String database, String username, String password) {
  11.         try {
  12.             driverClass = Class.forName("com.mysql.jdbc.Driver");
  13.             connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database + "?" +
  14.                                                      "user=" + username + "&password=" + password);
  15.             statement = connection.createStatement();
  16.         } catch(Exception e) {
  17.             e.printStackTrace();
  18.             return false;
  19.         } finally {
  20.             if(connection != null)
  21.                 Close();
  22.         }
  23.        
  24.         return true;
  25.     }
  26.    
  27.     public static void Close() {
  28.         try {
  29.             connection.close();
  30.             System.out.println("Disconnected from mysql");
  31.         } catch (Exception e) { }
  32.     }
  33.    
  34.     public static void Query(String query) {
  35.         try {
  36.             statement.executeUpdate(query);
  37.         } catch(SQLException sqle) {
  38.             sqle.printStackTrace();
  39.         }
  40.     }
  41.    
  42.     public static <T> void Insert(String table, String[] keys, T[] values) {
  43.         String execute;
  44.        
  45.         try {
  46.             execute = "INSERT INTO " + table;
  47.            
  48.             if(keys.length > 0) {
  49.                 execute += "(";
  50.                
  51.                 boolean firstTime = true;
  52.                 for(String key:keys) {
  53.                     if(!firstTime)
  54.                         execute += ",";
  55.                    
  56.                     execute += key;
  57.                     firstTime = false;
  58.                 }
  59.                
  60.                 execute += ")";
  61.             }
  62.            
  63.             execute += " VALUES(";
  64.            
  65.             boolean firstTime = true;
  66.             for(T value:values) {
  67.                 if(!firstTime)
  68.                     execute += ",";
  69.                
  70.                 execute += "\'" + value + "\'";
  71.                 firstTime = false;
  72.             }
  73.            
  74.             execute += ")";
  75.            
  76.             statement.executeUpdate(execute);
  77.         } catch(SQLException sqle) {
  78.             sqle.printStackTrace();
  79.         }
  80.     }
  81.    
  82.     public static String[][] Read(String table, String[] columns, String[] conditions, String[] condition_values, int limit) {
  83.         String execute = "";
  84.        
  85.         try {
  86.             execute = "SELECT ";
  87.            
  88.             boolean firstTime = true;
  89.             for(String column:columns) {
  90.                 if(!firstTime)
  91.                     execute += ",";
  92.                
  93.                 execute += column;
  94.                 firstTime = false;
  95.             }
  96.            
  97.             execute += " FROM " + table;
  98.            
  99.             if(conditions != null) {
  100.                 execute += " WHERE ";
  101.                
  102.                 int c = 0;
  103.                 boolean fTime = true;
  104.                 for(String condition:conditions) {
  105.                     if(!fTime)
  106.                         execute += ",";
  107.                    
  108.                     execute += condition + " = '" + condition_values[c] + "'";
  109.                     c++;
  110.                     fTime = false;
  111.                 }
  112.             }
  113.            
  114.             if(limit > 0) {
  115.                 execute += " LIMIT " + limit;
  116.             }
  117.            
  118.             int x = 0;
  119.             ResultSet resultSet = statement.executeQuery(execute);
  120.             while(resultSet.next()) { x++; }
  121.             String[][] results = new String[x][columns.length];
  122.            
  123.             ResultSet rs = statement.executeQuery(execute);
  124.             x = 0;
  125.             while(rs.next()) {
  126.                 for(int y = 0; y < columns.length; y++) {
  127.                     results[x][y] = rs.getString(y);
  128.                 }
  129.                 x++;
  130.             }
  131.            
  132.             return results;
  133.         } catch(Exception e) {
  134.             e.printStackTrace();
  135.             return new String[][]{};
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement