简体   繁体   English

具有PreparedStatement的JDBC批处理在MySQL中不起作用

[英]JDBC batch with PreparedStatement not working in MySQL

SPEC : MYSQL 5.7.16,JDK1.7,TOMCAT 8,mysql-connector-java-6.0.4.jar,WINDOWS 10 规格:MYSQL 5.7.16,JDK1.7,TOMCAT 8,mysql-connector-java-6.0.4.jar,WINDOWS 10

The Code below does not update batch method to db 下面的代码不会将批处理方法更新为db

Preparestatement pst = null;
String[] sqlx = {
"insert to abc('col1','col2')values('first', 'data')"
"insert to abc('col1','col2')values('second','data')"
"insert to abc('col1','col2')values('third', 'data')"
};

for(String sqIn : sqlx){
  pst = <jdbcConn>.preparestatement(sqIn);
  pst.addBatch();
}
int[] chkSql = pst.executeBatch();
//check if chkSql consists of 0..rollback else commit for > 0

On debug mode of the code, chkSql always has '1' ..this is dizzy ? 在代码的调试模式下,chkSql始终为'1'。 Only 1 row is inserted successfully , others are not inserted. 成功插入仅1行,未插入其他行。

Is this a bug with MYSQLDB or JAR ???? 这是MYSQLDB或JAR的错误吗?

It looks like you are creating a new PreparedStatement at each iteration, so the executeBatch() will only be applied to the last PreparedStatement object. 似乎您在每次迭代时都创建一个新的PreparedStatement ,因此executeBatch()将仅应用于最后一个PreparedStatement对象。

Furthermore, PreparedStatement is used to avoid SQL injection and it takes care of values escaping, when you use the ? 此外,使用PreparedStatement可以避免SQL注入,并且当您使用?时,它可以将值转义? placeholders system. 占位符系统。

The addBatch() method you are using is meant to work with variable parameters : 您正在使用的addBatch()方法适用于可变参数:

void addBatch() throws SQLException void addBatch()引发SQLException

Adds a set of parameters to this PreparedStatement object's batch of commands. 将一组参数添加到此PreparedStatement对象的命令批中。

, not with raw queries like you tried to do (for that, you would use addBatch(java.lang.String query) ,而不是尝试执行的原始查询(为此,您将使用addBatch(java.lang.String query)

void addBatch(String sql) throws SQLException void addBatch(String sql)抛出SQLException

Adds the given SQL command to the current list of commmands for this Statement object. 将给定的SQL命令添加到此Statement对象的当前命令列表中。 The commands in this list can be executed as a batch by calling the method executeBatch. 可以通过调用executeBatch方法来批量执行此列表中的命令。

The following example should do what you want : 下面的示例应执行所需的操作:

String[][] data = { { "first", "data" }, { "second", "data" }, { "third", "data" } };

String sql = "insert into abc(col1,col2) values (?, ?)";// use placeholders

PreparedStatement pst = connection.prepareStatement(sql);// create a single statement

for (String[] row : data) {

       // set parameters
       pst.setString(1, row[0]);
       pst.setString(2, row[1]);

       pst.addBatch();// validate the set
}

int[] chkSql = pst.executeBatch(); // execute the batch of commands
//check if chkSql consists of 0..rollback else commit for > 0
Statement stmt = null;
String[] sqlx = {
    "insert into abc(col1,col2) values('first', 'data')",
    "insert into abc(col1,col2) values('second','data')",
    "insert into abc(col1,col2) values('third', 'data')"
};
for(String sqIn : sqlx){
    stmt.addBatch(sqIn);
}
int[] chkSql = stmt.executeBatch();

try running this code. 尝试运行此代码。 connection is your Connection object. 连接是您的Connection对象。 also don't use a prepared statement if you don't want to set values at runtime. 如果您不想在运行时设置值,也不要使用预处理语句。

Have a look for this kind of queries here , 在这里看看这种查询,

and, prepared statements here 并且, 这里准备了语句

I hope this helps you to understand when to use what 我希望这可以帮助您了解何时使用什么

Have no idea about JDBC/Java but your posted INSERT statement is plain wrong. 不了解JDBC / Java,但是您发布的INSERT语句是完全错误的。 You should never quote the column names else it will be considered as string literal rather a column. 您永远不要引用列名,否则它将被视为字符串文字而不是列。 it should be 它应该是

insert to abc(col1,col2)values('first', 'data')

As in your posted code, it should look like 就像您发布的代码一样,

String[] sqlx = {
"insert into abc(col1,col2)values('first', 'data')",
"insert into abc(col1,col2)values('second','data')",
"insert into abc(col1,col2)values('third', 'data')"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM