diff --git a/pom.xml b/pom.xml index ac4e77e..1530eb6 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,11 @@ + + 1.8 + 1.8 + + com.konghq diff --git a/src/main/java/sql/SqlController.java b/src/main/java/sql/SqlController.java index e722dc2..75800c7 100644 --- a/src/main/java/sql/SqlController.java +++ b/src/main/java/sql/SqlController.java @@ -8,29 +8,25 @@ public class SqlController { private static Connection connection = null; - public static void connectSqlServer(){ - try{ - Class.forName("org.postgresql.Driver"); - connection = DriverManager - .getConnection("jdbc:postgresql://zipcode-group-project.cx9szw6knskg.us-east-1.rds.amazonaws.com:5432/stockprices", - "zcgroupproject", "cdhkvvkhdc"); - System.out.println("Opened database successfully"); - connection.setAutoCommit(false); - } catch (Exception e) { - e.printStackTrace(); - System.err.println(e.getClass().getName() + ": " + e.getMessage()); - System.exit(0); - } - System.out.println("Database still open"); - } +//<<<<<<< HEAD + public static void insertStock(String symbol, String month, Double open, Double high, Double low, Double close, Integer volume) throws SQLException { + SqlController.createTable(symbol); + String sql = "INSERT INTO " + symbol + " (ticker, month, open, high, low, close, volume) " + + "VALUES (?, ?, ?, ?, ?, ?, ?);"; - public static void insertStock(){ try { - Statement insert = connection.createStatement(); - String sql = "INSERT INTO _2020_03 (TICKER, OPEN, HIGH, LOW, CLOSE, VOLUME) " + - "VALUES ('MSFT', 165.3100, 175.0000, 138.5800, 145.7000, 636200296);"; - insert.executeUpdate(sql); - insert.close(); + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + preparedStatement.setString(1, symbol); + preparedStatement.setString(2, month); + preparedStatement.setDouble(3, open); + preparedStatement.setDouble(4, high); + preparedStatement.setDouble(5, low); + preparedStatement.setDouble(6, close); + preparedStatement.setInt(7, volume); + + preparedStatement.executeUpdate(); + preparedStatement.close(); connection.commit(); connection.close(); } catch (SQLException e) { @@ -66,27 +62,38 @@ public static TransactionMeta getStock(String stockSymbol, String month){ return builder.build(); } - public static void createTable() { - Statement statement = null; - try { + public static void connectSqlServer(){ + try{ Class.forName("org.postgresql.Driver"); connection = DriverManager .getConnection("jdbc:postgresql://zipcode-group-project.cx9szw6knskg.us-east-1.rds.amazonaws.com:5432/stockprices", "zcgroupproject", "cdhkvvkhdc"); - System.out.println("Opened database successfully"); + System.out.println("Opened database successfully in connect"); + connection.setAutoCommit(false); + } catch (Exception e) { + e.printStackTrace(); + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + System.exit(0); + } + System.out.println("Database remains open"); + } + public static void createTable(String symbol) { + connectSqlServer(); + Statement statement = null; + try { statement = connection.createStatement(); - String sql = "CREATE TABLE StockCall " + + String sql = "CREATE TABLE " + symbol + "(TICKER VARCHAR(10) PRIMARY KEY NOT NULL," + - " OPEN NUMERIC(4,2) NOT NULL, " + - " HIGH NUMERIC(4,2) NOT NULL, " + - " LOW NUMERIC(4,2) NOT NULL, " + - " CLOSE NUMERIC(4,2) NOT NULL," + + " MONTH VARCHAR(15) NOT NULL," + + " OPEN DECIMAL NOT NULL, " + + " HIGH DECIMAL NOT NULL, " + + " LOW DECIMAL NOT NULL, " + + " CLOSE DECIMAL NOT NULL," + " VOLUME BIGINT)"; statement.executeUpdate(sql); statement.close(); - connection.close(); - } catch (SQLException | ClassNotFoundException e ) { + } catch (SQLException e ) { System.err.println( e.getClass().getName()+": "+ e.getMessage() ); System.exit(0); } diff --git a/src/test/java/sql/SqlControllerTest.java b/src/test/java/sql/SqlControllerTest.java index fec62d2..2fdea41 100644 --- a/src/test/java/sql/SqlControllerTest.java +++ b/src/test/java/sql/SqlControllerTest.java @@ -3,6 +3,8 @@ import org.junit.Test; import stocks.TransactionMeta; +import java.sql.SQLException; + import static org.junit.Assert.*; public class SqlControllerTest { @@ -12,6 +14,12 @@ public void connectionTest(){ SqlController.connectSqlServer(); } + @Test + public void insertStockTest() throws SQLException { + //SqlController.connectSqlServer(); + SqlController.insertStock("QVC", "_2020-03", 165.2134, 175.0056, 56.9832, 138.5819, 636200296); + } + @Test public void getStockTest(){ TransactionMeta stock = SqlController.getStock("test", "_2020-03"); @@ -22,8 +30,8 @@ public void getStockTest(){ @Test public void createTableTest() { - //SqlController.connectSqlServer(); - SqlController.createTable(); - } +// SqlController.connectSqlServer(); + SqlController.createTable("QVC"); + } } \ No newline at end of file