Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package mysql_connector;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.sql.Types;
  11.  
  12. import com.google.gson.JsonArray;
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonParser;
  15. import com.google.gson.JsonPrimitive;
  16.  
  17. public class MySQL {
  18.     private Connection connection;
  19.    
  20.     public MySQL(String host, String port, String database, String user, String password) throws SQLException {
  21.         // constructor
  22.         this.connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
  23.     }
  24.    
  25.     public ResultSet execute(String sql, String parameters) throws SQLException {
  26.         ResultSet rs;
  27.         if (parameters != null && !parameters.isEmpty())
  28.         {
  29.             // PreparedStatement
  30.             PreparedStatement ps = this.getConnection().prepareStatement(sql);
  31.             JsonArray jo = new JsonParser().parse(parameters).getAsJsonArray();
  32.             int parameterIndex = 0;
  33.             for (final JsonElement je : jo)
  34.             {
  35.                 if (je.isJsonPrimitive())
  36.                 {
  37.                     JsonPrimitive jp = je.getAsJsonPrimitive();
  38.                     if (jp.isBoolean())
  39.                     {
  40.                         // Boolean
  41.                         ps.setBoolean(parameterIndex, jp.getAsBoolean());
  42.                     }
  43.                     else if (jp.isJsonNull())
  44.                     {
  45.                         // Null
  46.                         ps.setNull(parameterIndex, Types.NULL);
  47.                     }
  48.                     else if (jp.isNumber())
  49.                     {
  50.                         // Number
  51.                         // ... jp.getAsNumber();
  52.                     }
  53.                     else
  54.                     {
  55.                         // String
  56.                         ps.setString(parameterIndex, jp.getAsString());
  57.                     }
  58.                 }
  59.                 parameterIndex++;
  60.             }
  61.             rs = ps.executeQuery();
  62.         }
  63.         else
  64.         {
  65.             // Statement
  66.             Statement st = this.getConnection().createStatement();
  67.             rs = st.executeQuery(sql);
  68.         }
  69.         return rs;
  70.     }
  71.    
  72.     private enum ColumnTypes { NUMBER, BOOLEAN, STRING }
  73.    
  74.     public String fetch(ResultSet rs) throws SQLException, ClassNotFoundException {
  75.         ResultSetMetaData md = rs.getMetaData();
  76.        
  77.         ColumnTypes[] types = null;
  78.        
  79.         int l = md.getColumnCount();
  80.        
  81.         for (int i = 0; i < l; i++)
  82.         {
  83.             Class<?> ColumnClass = Class.forName(md.getColumnClassName(i));
  84.            
  85.             if (Number.class.isAssignableFrom(ColumnClass))
  86.             {
  87.                 // Number
  88.                 types[i] = ColumnTypes.NUMBER;
  89.             }
  90.             else if (Boolean.class.isAssignableFrom(ColumnClass))
  91.             {
  92.                 // Boolean
  93.                 types[i] = ColumnTypes.BOOLEAN;
  94.             }
  95.             else
  96.             {
  97.                 // String
  98.                 types[i] = ColumnTypes.STRING;
  99.             }
  100.         }
  101.        
  102.         // Collection ...?
  103.        
  104.         return null;
  105.     }
  106.    
  107.     public void close() throws SQLException {
  108.         // destructor
  109.         this.connection.close();
  110.     }
  111.  
  112.     public Connection getConnection() {
  113.         return connection;
  114.     }
  115.  
  116.     public void setConnection(Connection connection) {
  117.         this.connection = connection;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement