SQL表没有行

时间:2011-12-10 01:32:47

标签: java sql h2

下面的代码编译/运行没有错误。它创建了具有表TEST的db,但该表没有行。我究竟做错了什么?我可能搞乱了准备好的陈述,但无法弄清楚在哪里。请帮助,谢谢。

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 个答案:

答案 0 :(得分:8)

// Wrong
for (int i = 1; i>25; i++ )  { 
   ...
}

// Better
for (int i = 1; i <= 25; i++ )  { 
   ...
}