Java JDBC执行包

Java JDBC执行包,java,database,jdbc,Java,Database,Jdbc,我试图在数据库中执行许多INSERT命令 try{ int Records[]; Statement title = write.getConnection().createStatement(); Title titleFilter = application.Title.getTitle(); ResultSet rs = titleFilter.getTitleData(); while(rs.next()){; String ad

我试图在数据库中执行许多INSERT命令

try{
    int Records[];
    Statement title = write.getConnection().createStatement();
    Title titleFilter = application.Title.getTitle();
    ResultSet rs = titleFilter.getTitleData();

    while(rs.next()){;
        String add = ("INSERT INTO title VALUES ("
                + "'" + rs.getInt(1) + "'"+","
                + "'" +rs.getString(2)+ "'" +","
                + "'" +rs.getString(3) + "'"+","
                + "'" +rs.getInt(4)+ "'" +","
                + "'" +rs.getInt(5)+ "'" +","
                + "'" +rs.getInt(6) + "'"+","
                + "'" +rs.getString(7)+ "'" +","
                + "'" +rs.getInt(8) + "'"+","
                +"'" + rs.getInt(9)+ "'" +","
                + "'" +rs.getInt(10)+ "'" +","
                + "'" +rs.getString(11)+ "'" +","
                +"'" + rs.getString(12) + "'"+")"
        );
        title.addBatch(add);
        System.out.println(add);
        title.executeBatch();
    }
我知道在添加表达式后立即执行批处理有点愚蠢。我把它改了以找出我的错误

每次我尝试运行程序时,这个代码部分只插入六个表达式。为了找到我的错误,我改变了很多事情,但我想我永远也找不到了。此外,我得到这个例外

org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
  Position: 48
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2310)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2023)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:318)....

首先,你应该使用;这将帮助您避免语法错误(在连接Java
String
s时很难看到)。其次,您正在执行batch every循环,这违背了使用批处理的目的

下面是一个使用
PreparedStatement
的示例:

String sql = "INSERT INTO title VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement title = write.getConnection().prepareStatement(sql);
     ResultSet rs = titleFilter.getTitleData()) {

    while (rs.next()) {
        title.setInt(1, rs.getInt(1));
        title.setString(2, rs.getString(2));
        // ... do this for all the parameters ...

        title.addBatch(); // add to batch and move to next loop (if rs.next() returns true)
    }

    title.executeBatch(); // executed after loop

} catch (SQLException ex) {
    ex.printStackTrace(); // or do what you need to when an error occurs
}
本例还使用了

编辑:


正如Ivan在评论中提到的,最好在每个X记录之后执行批处理。我将把这段代码作为“读者练习”留下来;这将帮助您避免语法错误(在连接Java
String
s时很难看到)。其次,您正在执行batch every循环,这违背了使用批处理的目的

下面是一个使用
PreparedStatement
的示例:

String sql = "INSERT INTO title VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement title = write.getConnection().prepareStatement(sql);
     ResultSet rs = titleFilter.getTitleData()) {

    while (rs.next()) {
        title.setInt(1, rs.getInt(1));
        title.setString(2, rs.getString(2));
        // ... do this for all the parameters ...

        title.addBatch(); // add to batch and move to next loop (if rs.next() returns true)
    }

    title.executeBatch(); // executed after loop

} catch (SQLException ex) {
    ex.printStackTrace(); // or do what you need to when an error occurs
}
本例还使用了

编辑:


正如Ivan在评论中提到的,最好在每个X记录之后执行批处理。我将把这段代码留给“读者练习”。

哦,首先。。这只是从IDE复制过来的,不是我的代码中的吗?你可能应该使用here.use prepared语句,然后在while循环中设置参数。为什么你的整数值用引号括起来
“'”+rs.getInt(#)+“
在循环中使用
addBatch
+
executeBatch
有什么意义?您不妨调用
executeUpdate
。如果您想要批处理,请将
executeBatch
移出循环。--看在上帝的份上,使用
PreparedStatement
和参数标记,以防止攻击和失败的SQL语句。。这只是从IDE复制过来的,不是我的代码中的吗?你可能应该使用here.use prepared语句,然后在while循环中设置参数。为什么你的整数值用引号括起来
“'”+rs.getInt(#)+“
在循环中使用
addBatch
+
executeBatch
有什么意义?您不妨调用
executeUpdate
。如果需要批处理,请将
executeBatch
移动到循环外部。--看在上帝的份上,使用
PreparedStatement
和参数标记,以防止攻击和失败的SQL语句。根据
ResultSet
返回的记录数,最好在每个X条记录之后执行批处理。我想我用这种方法解决了问题。。。4.出现了另一个问题,但我想我自己能解决。。感谢大家:)根据
ResultSet
返回的记录数,最好在每个X条记录之后执行批处理。我想我用这种方法解决了这个问题。。。4.出现了另一个问题,但我想我自己能解决。。谢谢大家:)