下面的代码编译/运行没有错误。它创建了具有表 TEST
的数据库,但该表没有行。我究竟做错了什么?我可能搞砸了准备好的声明,但不知道在哪里。请帮忙,谢谢。
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);
}
}
}
}
最佳答案
// Wrong
for (int i = 1; i>25; i++ ) {
...
}
// Better
for (int i = 1; i <= 25; i++ ) {
...
}
关于java - SQL 表没有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8453690/