The code below compiles/runs without errors. It created the db which has the table TEST but the table has no rows. What am I doing wrong? I may be messing up the prepared statement, but can’t figure out where. Please help, thanks.
public class H2Test {
public static void main (String[] args) throws ClassNotFoundException, Exception {
Class.forName("org.h2.Driver");
Connection conn = null;
String path = "E:\\Dropbox\\Personal\\Development\\BlueJ examples\\sql_test\\";
String dbName = "h2db";
String s2 = "s2";
String s3 = "s3";
try {
conn = DriverManager.getConnection("jdbc:h2:" + path + dbName, "sa", "");
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
statement.setQueryTimeout(30);
statement.executeUpdate("DROP TABLE IF EXISTS TEST;");
statement.executeUpdate("CREATE TABLE TEST (id IDENTITY PRIMARY KEY, name VARCHAR(255), job VARCHAR(255));");
PreparedStatement prep = conn.prepareStatement ( "insert into TEST values (?,?,?);" );
for (int i = 1; i>25; i++ ) {
prep.setInt(1, i);
prep.setString(2, s2);
prep.setString(3, s3);
prep.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
}
}
catch(SQLException e) {
System.err.println(e.getMessage());
}
finally {
try {
if(conn != null)
conn.close();
}
catch(SQLException e) {
System.err.println(e);
}
}
}
}
1 Answer