gpt4 book ai didi

java - SQL 表没有行

转载 作者:行者123 更新时间:2023-12-01 08:46:35 25 4
gpt4 key购买 nike

下面的代码编译/运行没有错误。它创建了具有表 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com