Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. public class Database {
  9.  
  10.     private static String database, instance, username, password, jdbcUrl;
  11.     private static Connection connection;
  12.     private static String[] optionColumns;
  13.  
  14.     static {
  15.         database = "OptionsSpreadsheet";
  16.         instance = "optionsspreadsheetprogram:us-central1:optionsspreadsheet";
  17.         username = "root";
  18.         password = "Olibslic1234";
  19.         jdbcUrl = String.format(
  20.                 "jdbc:mysql://google/%s?cloudSqlInstance=%s&"
  21.                         + "socketFactory=com.google.cloud.sql.mysql.SocketFactory", database, instance);
  22.  
  23.         try {
  24.             connection = DriverManager.getConnection(jdbcUrl, username, password);
  25.  
  26.             String getColumnQuery = "SHOW COLUMNS FROM optionsQuotes";
  27.             List<HashMap> queryResult = query(getColumnQuery);
  28.  
  29.             optionColumns = new String[queryResult.size()];
  30.             for(int i = 0; i < queryResult.size(); i++) {
  31.                 optionColumns[i] = queryResult.get(i).get("COLUMN_NAME").toString();
  32.             }
  33.         } catch (SQLException e) {
  34.             e.printStackTrace();
  35.         }
  36.  
  37.     }
  38.  
  39.     public static List<HashMap> query(String query) throws SQLException {
  40.         ArrayList results = new ArrayList();
  41.  
  42.         if(connection != null && connection.isValid(0)) {
  43.             try(Statement statement = connection.createStatement()) {
  44.                 ResultSet resultSet = statement.executeQuery(query);
  45.                 ResultSetMetaData md = resultSet.getMetaData();
  46.                 int columnCount = md.getColumnCount();
  47.  
  48.                 while(resultSet.next()) {
  49.                     HashMap row = new HashMap(columnCount);
  50.                     for(int i = 1; i<=columnCount; ++i) {
  51.                         row.put(md.getColumnName(i), resultSet.getObject(i));
  52.                     }
  53.                     results.add(row);
  54.                 }
  55.                 return results;
  56.             }
  57.         }
  58.         System.out.println("CONNECTION NOT FORMED");
  59.         return null;
  60.     }
  61.  
  62.     public static void insertOptionList(List<HashMap> options) throws Exception {
  63.         clearTable("optionsQuotes");
  64.  
  65.         String putCommand = "INSERT INTO optionsQuotes VALUES (";
  66.  
  67.         for(int i = 0; i < optionColumns.length-1; i++) {
  68.             putCommand += "?, ";
  69.         }
  70.         putCommand += "?)";
  71.  
  72.         PreparedStatement putStatement = connection.prepareStatement(putCommand);
  73.  
  74.         for(HashMap option: options) {
  75.             for(int i = 1; i <= optionColumns.length; i++) {
  76.                 putStatement.setObject(i, option.get(optionColumns[i-1]));
  77.             }
  78.             putStatement.addBatch();
  79.             putStatement.clearParameters();
  80.         }
  81.  
  82.         putStatement.executeBatch();
  83.     }
  84.  
  85.     public static void clearTable(String table) throws SQLException {
  86.         if(connection != null && connection.isValid(0)) {
  87.             try(Statement statement = connection.createStatement()) {
  88.                 statement.executeUpdate("TRUNCATE " + table);
  89.             }
  90.         }
  91.     }
  92.  
  93.     public static void insertValues(String table, String[] values) throws Exception{
  94.         String command = "INSERT INTO " + table + " VALUES (";
  95.  
  96.         for(String value: values) {
  97.             command += value + ", ";
  98.         }
  99.  
  100.         command += ")";
  101.  
  102.         if(connection != null && connection.isValid(0)) {
  103.             try(Statement statement = connection.createStatement()) {
  104.                 statement.executeUpdate(command);
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement