具有PreparedStatement的JDBC批处理在MySQL中不起作用 [英] JDBC batch with PreparedStatement not working in MySQL

查看:130
本文介绍了具有PreparedStatement的JDBC批处理在MySQL中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面的代码不会将批处理方法更新为db

The Code below does not update batch method to 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

在代码的调试模式下,chkSql始终为'1'. 成功插入仅1行,未插入其他行.

On debug mode of the code, chkSql always has '1' ..this is dizzy ? Only 1 row is inserted successfully , others are not inserted.

这是MYSQLDB或JAR的错误吗??

Is this a bug with MYSQLDB or JAR ????

推荐答案

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();

尝试运行此代码.连接是您的Connection对象.如果您不想在运行时设置值,也不要使用预处理语句.

try running this code. connection is your Connection object. also don't use a prepared statement if you don't want to set values at runtime.

此处中查找此类查询,

和准备好的语句此处

我希望这可以帮助您了解何时使用什么

I hope this helps you to understand when to use what

这篇关于具有PreparedStatement的JDBC批处理在MySQL中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆