源代码:
try{
Connection con=DriverManager.getConnection("jdbc:odbc:db1","root","");
System.out.println("Successs Linked DataBase");
Statement stm1=con.createStatement();
                  stm1.addBatch("insert into pet values ('S1','Zl','dog','f',null,null)");
                  stm1.addBatch("update pet set pet.name='S0' where pet.name='S1'");
                  stm1.executeBatch();
                  stm1.executeBatch();

}
catch(Exception ex){
ex.printStackTrace();
}
sun.jdbc.odbc.JdbcOdbcBatchUpdateException: [MySQL][ODBC 5.1 Driver][mysqld-5.1.40-community]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 'update pet set pet.name='S0' where pet.name='S1'' at line 2
at sun.jdbc.odbc.JdbcOdbcStatement.executeBatchUpdate(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeBatch(Unknown Source)
at cn.test.ExecuteBatchTest.main(ExecuteBatchTest.java:46)
找了好久还是没答案。是数据库驱动问题?数据库?头疼啊。

解决方案 »

  1.   

    你把这句stm1.addBatch("update pet set pet.name='S0' where pet.name='S1'"); 
    先去掉执行一次
    看看能不能执行
      

  2.   

    You have an error in your SQL syntax
    你的SQL语句有错误update pet set pet.name='S0' where pet.name='S1'
    去掉pet.再看看  或者在pet后面加个别名 再用别名.name
      

  3.   

    SQL语句都没问题
    就是要去掉一个stm1.addBatch("****");
    就是只有在加一个的情况下才能运行
    你们以前用过Statement.addBatch()方法没?用的是什么数据库?
      

  4.   

    jdk1.5以后的版本才支持批处理,检查下你的jdk版本。
      

  5.   

    stm1.executeBatch();
    stm1.executeBatch(); 
    为什么要执行两次呢????
      

  6.   

    发帖的时候写错的不影响
    今天换了个SQl Server2000可以用batch方法了(说明应该是驱动问题)
    虽然命令可以执行
    也加进数据库了
    但还是有异常:SQL Attempt to produce a ResultSet from executeBatch
    且executeBatch()方法没有返回结果
    另外就是用PreparedStatement的pstmt.setDate(9,null);方法抛出异常:没有执行可选特性
    网上找了有的说是驱动问题 换驱动桥接方式
      

  7.   

    batch就是批处理, 你这叫什么批处理?我给你写个例子你看看:public class Test {
    public static final int BATCH_SIZE = 100;
    public static void main(String[] args) throws SQLException{
    String sql = "update some_table set column1=?, column2=? where condition=?";
    Connection conn = null;
    PreparedStatement ps = null;
    int count = 0;
    conn.setAutoCommit(false);
    ps = conn.prepareStatement(sql);
    for(int i=0; i<1200; i++){
    ps.setInt(1, i);
    ps.setLong(2, i*i);
    ps.setInt(3, i);
    ps.addBatch();
    count++;
    if(count==BATCH_SIZE){
    ps.executeBatch();
    count=0;
    }
    }
    if (count > 0) {
                ps.executeBatch();
            }
    conn.commit();
    }
    }当时这个需求需要根据计算结果更新8000多条信息,不用batch的话执行了3分钟,用了batch之后只需要18秒
      

  8.   

    stm1.executeBatch(); 
    为什么要执行2次?
      

  9.   

    怎么没见
    Class.format("sun.jdbc.odbc.JdbcOdbcDriver");?