· 6 years ago · Mar 16, 2019, 07:46 AM
1private boolean tableExists() throws SQLException {
2 System.out.println("tableExists()");
3
4 DatabaseMetaData dbmd = conn.getMetaData();
5 ResultSet rs = dbmd.getTables(null, null, this.getTableName(), null);
6
7 System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME"));
8
9 return rs.getRow() == 1;
10}
11
12private void createTable() throws SQLException {
13 String sqlCreate = "CREATE TABLE IF NOT EXISTS " + this.getTableName()
14 + " (brand VARCHAR(10),"
15 + " year INTEGER,"
16 + " number INTEGER,"
17 + " value INTEGER,"
18 + " card_count INTEGER,"
19 + " player_name VARCHAR(50),"
20 + " player_position VARCHAR(20))";
21
22 Statement stmt = conn.createStatement();
23 stmt.execute(sqlCreate);
24}
25
26public void testConstructor() throws Exception {
27 try (BaseballCardJDBCIO bcdb = new BaseballCardJDBCIO(this.url)) {
28 String query = "SELECT count(*) FROM information_schema.system_tables WHERE table_name = '" + bcdb.getTableName() + "'";
29 Connection conn = DriverManager.getConnection(this.url);
30 Statement stmt = conn.createStatement();
31 ResultSet rs = stmt.executeQuery(query);
32 Assert.assertTrue(rs.next());
33 Assert.assertEquals(1, rs.getInt(1));
34 Assert.assertFalse(rs.next());
35 }
36}
37
38FAILED: expected: <1> but was: <0>
39
40private void createTable() throws SQLException {
41 String sqlCreate = "CREATE TABLE IF NOT EXISTS " + this.getTableName()
42 + " (brand VARCHAR(10),"
43 + " year INTEGER,"
44 + " number INTEGER,"
45 + " value INTEGER,"
46 + " card_count INTEGER,"
47 + " player_name VARCHAR(50),"
48 + " player_position VARCHAR(20))";
49
50 Statement stmt = conn.createStatement();
51 stmt.execute(sqlCreate);
52}