From 4c6ced707b3c54ca513c25663fcacf91c27479a4 Mon Sep 17 00:00:00 2001 From: chipfody <60615532+chipfody@users.noreply.github.com> Date: Wed, 18 Mar 2020 09:28:14 -0400 Subject: [PATCH 1/3] saved work - create and add --- src/main/java/sql/SqlController.java | 23 ++++++++++++++++------- src/test/java/sql/SqlControllerTest.java | 8 +++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/sql/SqlController.java b/src/main/java/sql/SqlController.java index 05c652b..5b93d8a 100644 --- a/src/main/java/sql/SqlController.java +++ b/src/main/java/sql/SqlController.java @@ -23,11 +23,18 @@ public static void connectSqlServer(){ } public static void insertStock(){ + //Connection connection = null; + //String ticker = "MSFT"; + //SqlController.createTable(); + String sql = "INSERT INTO AMZN (TICKER, MONTH, OPEN, HIGH, LOW, CLOSE, VOLUME) " + + "VALUES ('MSFT', '_2020_03',165.31, 175.00, 138.58, 145.70, 636200296);"; + //Statement statement = null; try { + //assert connection != null; 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); + System.out.println(sql); insert.close(); connection.commit(); connection.close(); @@ -70,12 +77,14 @@ public static void createTable() { System.out.println("Opened database successfully"); statement = connection.createStatement(); - String sql = "CREATE TABLE StockCall " + + String stockName = "AMZN"; + String sql = "CREATE TABLE " + stockName + "(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(); diff --git a/src/test/java/sql/SqlControllerTest.java b/src/test/java/sql/SqlControllerTest.java index a302315..b4aeba5 100644 --- a/src/test/java/sql/SqlControllerTest.java +++ b/src/test/java/sql/SqlControllerTest.java @@ -11,6 +11,12 @@ public void connectionTest(){ SqlController.connectSqlServer(); } + @Test + public void insertStockTest() { + SqlController.connectSqlServer(); + SqlController.insertStock(); + } + @Test public void getStockTest(){ SqlController.connectSqlServer(); @@ -19,7 +25,7 @@ public void getStockTest(){ @Test public void createTableTest() { - //SqlController.connectSqlServer(); +// SqlController.connectSqlServer(); SqlController.createTable(); } From b68a5d132264c743ff38d697128805ccc9774c0d Mon Sep 17 00:00:00 2001 From: chipfody <60615532+chipfody@users.noreply.github.com> Date: Wed, 18 Mar 2020 14:42:53 -0400 Subject: [PATCH 2/3] saved pre-pull of Han's --- src/main/java/sql/SqlController.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/sql/SqlController.java b/src/main/java/sql/SqlController.java index 5b93d8a..057dcc5 100644 --- a/src/main/java/sql/SqlController.java +++ b/src/main/java/sql/SqlController.java @@ -4,6 +4,7 @@ public class SqlController { private static Connection connection = null; + private String symbol; public static void connectSqlServer(){ try{ @@ -24,7 +25,7 @@ public static void connectSqlServer(){ public static void insertStock(){ //Connection connection = null; - //String ticker = "MSFT"; + String symbol = "MSFT"; //SqlController.createTable(); String sql = "INSERT INTO AMZN (TICKER, MONTH, OPEN, HIGH, LOW, CLOSE, VOLUME) " + "VALUES ('MSFT', '_2020_03',165.31, 175.00, 138.58, 145.70, 636200296);"; @@ -77,8 +78,8 @@ public static void createTable() { System.out.println("Opened database successfully"); statement = connection.createStatement(); - String stockName = "AMZN"; - String sql = "CREATE TABLE " + stockName + + String symbol = "AMZN"; + String sql = "CREATE TABLE " + symbol + "(TICKER VARCHAR(10) PRIMARY KEY NOT NULL," + " MONTH VARCHAR(15) NOT NULL," + " OPEN DECIMAL NOT NULL, " + From cd5fefebf4d3da2a48ca763c0f485d02aa702aca Mon Sep 17 00:00:00 2001 From: chipfody <60615532+chipfody@users.noreply.github.com> Date: Thu, 19 Mar 2020 11:56:59 -0400 Subject: [PATCH 3/3] functionality in connect, create, and insert into table --- pom.xml | 5 ++ src/main/java/sql/SqlController.java | 67 +++++++++++------------- src/test/java/sql/SqlControllerTest.java | 12 +++-- 3 files changed, 43 insertions(+), 41 deletions(-) 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 aa3170a..fecf050 100644 --- a/src/main/java/sql/SqlController.java +++ b/src/main/java/sql/SqlController.java @@ -7,39 +7,25 @@ public class SqlController { private static Connection connection = null; - private String symbol; - 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"); + public static void insertStock(String symbol, String month, Double open, Double high, Double low, Double close, Integer volume) { + SqlController.createTable(symbol); + String sql = "INSERT INTO " + symbol + " (ticker, month, open, high, low, close, volume) " + + "VALUES (?, ?, ?, ?, ?, ?, ?);"; - } - - public static void insertStock(){ - //Connection connection = null; - String symbol = "MSFT"; - //SqlController.createTable(); - String sql = "INSERT INTO AMZN (TICKER, MONTH, OPEN, HIGH, LOW, CLOSE, VOLUME) " + - "VALUES ('MSFT', '_2020_03',165.31, 175.00, 138.58, 145.70, 636200296);"; - //Statement statement = null; try { - //assert connection != null; - Statement insert = connection.createStatement(); + 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); - insert.executeUpdate(sql); - System.out.println(sql); - insert.close(); + preparedStatement.executeUpdate(); + preparedStatement.close(); connection.commit(); connection.close(); } catch (SQLException e) { @@ -74,17 +60,27 @@ 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 symbol = "AMZN"; String sql = "CREATE TABLE " + symbol + "(TICKER VARCHAR(10) PRIMARY KEY NOT NULL," + " MONTH VARCHAR(15) NOT NULL," + @@ -95,8 +91,7 @@ public static void createTable() { " 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 0c87d5a..c372704 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 { @@ -13,9 +15,9 @@ public void connectionTest(){ } @Test - public void insertStockTest() { - SqlController.connectSqlServer(); - SqlController.insertStock(); + public void insertStockTest() throws SQLException { + //SqlController.connectSqlServer(); + SqlController.insertStock("QVC", "_2020-03", 165.2134, 175.0056, 56.9832, 138.5819, 636200296); } @Test @@ -30,7 +32,7 @@ public void getStockTest(){ @Test public void createTableTest() { // SqlController.connectSqlServer(); - SqlController.createTable(); - } + SqlController.createTable("QVC"); + } } \ No newline at end of file