我用JDBC执行一条update语句时,能够成功执行,可是我执行两条或者多条update语句时却出现错误咯
1、sql = "update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1"; // 正确2. sql = "update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1; update monitor_id set lastid = 200 ,now_max_id = 200 where id = 2;"; // 出错
错误信息:
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2972)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.Statement.execute(Statement.java:529)哪位高手能帮忙解释一下啊 谢谢咯 先。。
1、sql = "update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1"; // 正确2. sql = "update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1; update monitor_id set lastid = 200 ,now_max_id = 200 where id = 2;"; // 出错
错误信息:
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2972)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.Statement.execute(Statement.java:529)哪位高手能帮忙解释一下啊 谢谢咯 先。。
解决方案 »
- 用Java写的遍历目录下面文件,总是出错
- java MenuItem 怎样增加Icon图像呢?
- 有谁会用java写农历转公历
- java 实现发送Email的守护进程
- CSDN,怎么让这种垃圾当版主?
- 哪位信息灵通人士知道有没有jdk1.4 API中文版?...
- 求阶层代码
- 各位高手我是一个java的初学者我用JCreator Pro编译文件是老是出现:"You must first create a JDK profile"不知如何解决?
- 小妹问大家一个问题!求你们了!踊跃点!UP也有分的!!
- 有没有人有兴趣讨论一下北京java programmer的工资?
- 一道程序设计题....用java如何实现
- 关于java用hibernate调用数据库的问题。
为什么呢 ??? sql语句不是一条字符串吗??
insert 语句是可以连续写多条的丫 然后执行一次就OK咯 难得update需要一条语句执行一次 是不是效率太低咯 时间都浪费在路上咯 可是 (⊙o⊙)
PreparedStatement 或 Statement 应该都是一句句执行,不可以进行批处理的个人理解,希望有帮助
多多交流
如果我要是更新一千万条记录的话,是不是要向数据库发送一千万次sql语句哦 这样子的话
更新一千万条数据也不会是一条一条去更新的,一般 UPDATE 语句用于批量更新,比如说统一修改某些数据。
“sql = "update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1; update monitor_id set lastid = 200 ,now_max_id = 200 where id = 2;";”
有换行?如果按照我之前的错误来看呢···你不要换行就对了···把这句sql写成一行。
Statement stmt = null;
String[] sqls = {
"update monitor_id set lastid = 11 ,now_max_id = 11177 where id = 1",
"update monitor_id set lastid = 200 ,now_max_id = 200 where id = 2"
}; for (int i = 0; i < sqls.length; i++) {
try {
stmt.addBatch(sqls[i]);
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
try {
stmt.executeBatch();
} catch (SQLException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
你执行一条sql后没有关闭就接着执行第二条,结果集也没有清空,貌似会出错吧
string sql2 = "";