-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateStockQuotesTable.java
More file actions
59 lines (50 loc) · 2.02 KB
/
Copy pathcreateStockQuotesTable.java
File metadata and controls
59 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package classes;
import java.sql.*;
public class createStockQuotesTable {
private static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static String DB_URL = "jdbc:mysql://localhost:3306/cs565";
private static String DB_Account_IDNAME = "cs";
private static String DB_PASSWORD = "java";
public static void main(String[] args) {
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sqlDropStatement;
String sqlCreateStatement;
String sql;
//drop the StockQuotes table if it exist already.
sqlDropStatement = "DROP TABLE StockQuotes";
try {
stmt.executeUpdate(sqlDropStatement);
}catch (Exception e1) {
System.out.println("Trying to drop StockQuotes Table...");
}
//create the StockQuotes table
sqlCreateStatement = "CREATE TABLE StockQuotes " + "(StockSymbol varchar(8) primary key,"
+ " CurrentStockPrice decimal(6, 2))ENGINE=INNODB";
stmt.executeUpdate(sqlCreateStatement);
/*
String clearTableStatement = "delete from StockQuotes";
try {
stmt.executeUpdate(clearTableStatement);
}catch (Exception e1) {
System.out.println("Clearing old transaction information in memory");
}*/
//insert data to the StockQuotes
double factor;
factor=Math.random()*2.0;
sql = "insert into StockQuotes (StockSymbol, CurrentStockPrice) values ('AAA', 100.00*'"+factor+"')";
stmt.executeUpdate(sql);
sql = "insert into StockQuotes (StockSymbol, CurrentStockPrice) values ('BBB', 18.00*'"+factor+"')";
stmt.executeUpdate(sql);
sql = "insert into StockQuotes (StockSymbol, CurrentStockPrice) values ('CCC', 218.09*'"+factor+"')";
stmt.executeUpdate(sql);
sql = "insert into StockQuotes (StockSymbol, CurrentStockPrice) values ('DDD', 650.00*'"+factor+"')";
stmt.executeUpdate(sql);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}