PreparedStatement prst = null;
Statement st = null;
Connection conn = null;
try {
Class.forName("SQLite.JDBCDriver").newInstance();
if (dbFile.exists()) {
dbFile.delete();
}
conn = DriverManager.getConnection("jdbc:sqlite:/"
+ dbFile.getPath());
assertNotNull(conn);
// create table
st = conn.createStatement();
String sql = "CREATE TABLE zoo (ZID INTEGER NOT NULL, family VARCHAR (20) NOT NULL, name VARCHAR (20) NOT NULL, PRIMARY KEY(ZID) )";
st.executeUpdate(sql);
String update = "update zoo set family = ? where name = ?;";
prst = conn.prepareStatement(update);
prst.setString(1, "cat");
prst.setString(2, "Yasha");
// st = conn.createStatement();
// st.execute("select * from zoo where family = 'cat'");
// ResultSet rs = st.getResultSet();
// assertEquals(0, getCount(rs));
prst.executeUpdate();
// st.execute("select * from zoo where family = 'cat'");
// ResultSet rs1 = st.getResultSet();
// assertEquals(1, getCount(rs1));
try {
prst = conn.prepareStatement("");
prst.execute();
fail("SQLException is not thrown");
} catch (SQLException e) {
// expected
}
try {
conn.prepareStatement(null);
fail("NPE is not thrown");
} catch (Exception e) {
// expected
}
try {
st = conn.createStatement();
st.execute("drop table if exists zoo");
} catch (SQLException e) {
fail("Couldn't drop table: " + e.getMessage());
} finally {
try {
st.close();
conn.close();
} catch (SQLException ee) {
}
}
} finally {
try {
if (prst != null) {
prst.close();
}
if (st != null) {
st.close();
}
} catch (SQLException ee) {
}
}