【问题标题】:SQL table has no rowsSQL 表没有行
【发布时间】:2011-12-10 01:32:46
【问题描述】:

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

【问题讨论】:

    标签: java sql h2


    【解决方案1】:
    // Wrong
    for (int i = 1; i>25; i++ )  { 
       ...
    }
    
    // Better
    for (int i = 1; i <= 25; i++ )  { 
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      相关资源
      最近更新 更多