这是我自己封装的一个连接数据库的javabean:package axtic.db;import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBOperation
{
    private Connection conn = null;
    private Statement stmt = null;
    ResultSet rs = null;
    /** 读取数据库驱动
     *  
     */ 
    public void loadPool() throws SQLException,ClassNotFoundException
    {
        Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
        conn = DriverManager.getConnection("proxool.xml-test");
    }
    
    public DBOperation() throws SQLException,ClassNotFoundException
    { 
        loadPool();       
    }
    
    /** 数据库查询
     *  sql:SQL查询语句
     */    
    public ResultSet select(String sql) throws SQLException
    {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        return rs;
    }
    
    /** 数据库添加、修改、删除
     *  sql:SQL语句
     */
    public void update(String sql) throws SQLException
    {
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
    }
    
    /** 数据库批量添加、修改
     *  sql:SQL语句
     */
     public void BatchUpdate(String sql) throws SQLException,BatchUpdateException
     {
       stmt = conn.createStatement();
       stmt.addBatch(sql);
       stmt.executeBatch();
     }
    
    /** 关闭数据集
     */
     public void closestmt() throws SQLException
     {
            stmt.close();
     }     
    
    /** 关闭数据库连接
     */
     public void closeconn() throws SQLException
     {
            conn.close();    
     }
}
在这里我封装了一个批量更新BatchUpdate(String sql),使用的时候不起作用?各位帮我改改?先谢拉!!!

解决方案 »

  1.   

    怎么个“不起作用”?是没有实现“批”的效果?你这中用法实际就是每个语句单独作为一批,所以没有实际效果。是不是语句根本没执行?那要看你用的 driver 了,JDBC 接口规范里说,executeBatch() 是不要求一定实现的。
      

  2.   

    <%
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
    conn = DriverManager.getConnection("proxool.xml-test");
    stmt = conn.createStatement();
    try
    {
      conn.setAutoCommit(false);
      Boolean bError = false;
      stmt.clearBatch();
      
      stmt.addBatch("update td2 SET tdone='872916' where tdone='null'");
      stmt.addBatch("update td2 SET tdone='872917' where tdone='null'");
      stmt.addBatch("update td2 SET tdone='872918' where tdone='null'");
      
      int[] aiupdateCounts = stmt.executeBatch();
      
    }
    catch(BatchUpdateException bue)
    {
      Boolean bError = true;
      int[] aiupdateCounts = bue.getUpdateCounts();
      SQLException SQLe = bue;
      while( SQLe != null)
      {
        SQLe = SQLe.getNextException();
      }  
    }
    catch(SQLException SQLe)
    {
      SQLe.printStackTrace();
    }
    conn.close();
    stmt.close();
    %>
    这样写可以批量更新了,但是还是没有实现我想要的效果。我想实现的是这样的效果:
    有一批货物,除了订单号不相同,其他所有信息相同,假设为50件,我现在批量添加了50件货物进数据库,然后我输入所有的订单号,更新到50件货物信息里面去。