Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
</plugins>
</build>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.konghq</groupId>
Expand Down
71 changes: 39 additions & 32 deletions src/main/java/sql/SqlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/sql/SqlControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Test;
import stocks.TransactionMeta;

import java.sql.SQLException;

import static org.junit.Assert.*;

public class SqlControllerTest {
Expand All @@ -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");
Expand All @@ -22,8 +30,8 @@ public void getStockTest(){

@Test
public void createTableTest() {
//SqlController.connectSqlServer();
SqlController.createTable();
}
// SqlController.connectSqlServer();
SqlController.createTable("QVC");
}

}