jdbc批处理更新在多个语句上失败

时间:2018-12-23 11:23:17

标签: java mysql jdbc

我正在Java线程上运行对mysql数据库的更新,而我遇到了一个奇怪的问题:如果批处理中有4条或更少的语句,那么它就毫无问题了,但是如果我有5条或更多信息我正在获取sql语法异常,并且只有第一条语句行不通。我已经用相同的语句尝试过此操作,因此我很确定语法是正确的。谁能帮助我了解发生了什么事?

对于jdbc驱动程序,我正在使用<li><%= link_to "page", page_path %> 中的com.mysql.cj.jdbc.Driver,该软件包与mysql一起打包。这是相关课程的代码:

mysql-connector-java-8.0.12.jar

这是我从打印语句获得的输出,但有一个例外:

public class DBwriteThread extends Thread {

private final Statement state;
//execute batch after number of statements
public static final int BATCH_EXEC_SIZE = 5;

//# of strings added to the current batch
private int queryC;
private String tmpQueryStr;

private final BlockingQueue<String> queryStrQueue;

public DBwriteThread(Connection con, BlockingQueue<String> q) throws SQLException{
    this.queryC = 0;

    setName("DBwriteThread");

    state = con.createStatement();
    queryStrQueue = q;
}

@Override
public void run() {
    System.out.println(Thread.currentThread().getName() + " runs");
    while(true){
        try {
                tmpQueryStr = queryStrQueue.take(); //take blocks the thread

                System.out.println(tmpQueryStr);
                if (tmpQueryStr != "fc"){
                    state.addBatch(tmpQueryStr);
                    queryC++;
                }

                if(queryC >= BATCH_EXEC_SIZE || tmpQueryStr == "fc"){ 
                    System.out.println(state.toString());
                    state.executeBatch();
                    reportCommit();
                    queryC = 0;
                }
        } 
        catch (SQLException ex) {
            System.err.println(ex.getSQLState());
            ex.printStackTrace();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } 
    }
} 
[...]
}

以上示例中的表的设置如下:

//output 4 statements:
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
fc
com.mysql.cj.jdbc.StatementImpl@5123cf89
DBwriteThread: commiting 4 statements to database


//output 5 statements:
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdasd99as");
com.mysql.cj.jdbc.StatementImpl@5123cf89
42000
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdas' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.util.Util.handleNewInstance(Util.java:210)
    at com.mysql.cj.util.Util.getInstance(Util.java:185)
    at com.mysql.cj.util.Util.getInstance(Util.java:192)
    at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224)
    at com.mysql.cj.jdbc.StatementImpl.executeBatchUsingMultiQueries(StatementImpl.java:1052)
    at com.mysql.cj.jdbc.StatementImpl.executeBatchInternal(StatementImpl.java:860)
    at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:813)
    at shop.db.DBwriteThread.run(DBwriteThread.java:59)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';INSERT INTO products VALUES ( NULL,"prod 9","90.33","5","1","some text99dfasdas' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:781)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:798)
    at com.mysql.cj.jdbc.StatementImpl.executeBatchUsingMultiQueries(StatementImpl.java:1038)
    ... 3 more

0 个答案:

没有答案