Advertisement
Guest User

Untitled

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