public void batRunRequest(String _toApplicationId, String _toEventId, String _fromEventId, String _userId) throws DataConnectException, DataAccessException, DataPropertyException {
Connection con = getConnection();
String jobId = null;
String jobGroupId = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String systemDate = dateFormat.format(new Date());
try {
con.setAutoCommit(false);
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement(JOB_ID_SEQ);
rs = pst.executeQuery();
while (rs.next()) {
jobId = systemDate + String.format("%07d", Integer.value(rs.getString(1)));
}
} finally {
try{
if (rs != null) {
rs.close();
}
}finally{
if (pst != null) {
pst.close();
}
}
} try {
pst = con.prepareStatement(JOB_GROUP_ID_SEQ);
rs = pst.executeQuery();
while (rs.next()) {
jobGroupId = systemDate + String.format("%07d", Integer.valueOf(rs
.getString(1)));
}
} finally {
try{
if (rs != null) {
rs.close();
}
}finally{
if (pst != null) {
pst.close();
}
}
} try {
pst = con.prepareStatement(SQL_BAT_KANRI_INSERT);
pst.setString(1, jobGroupId);
pst.setString(2, jobId);
pst.setString(3, _toApplicationId);
pst.setString(4, _toEventId);
pst.setString(5, _fromEventId);
pst.setString(6, _userId);
pst.executeUpdate();
} finally {
if (pst != null) {
pst.close();
}
} try {
pst = con.prepareStatement(SQL_BAT_PARAMETER_INSERT);
pst.setString(1, jobId);
pst.executeUpdate();
con.commit();
} finally {
if (pst != null) {
pst.close();
}
}
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException e1) {
throw new DataAccessException(e.getMessage(), e);
}
}
throw new DataAccessException(e.getMessage(), e);
}
}

解决方案 »

  1.   

    请问下,我在上面将statement关闭后,再COMMIT为什么没有问题,回滚也是OK的,是不是说JDBC中,statement关闭后,con.commit再执行是没有问题的
      

  2.   

    各位能不能提供一段完整的,操作多个STATEMENT的例子
      

  3.   

    再使用多个STATEMENT的这里,总是感觉不好使
      

  4.   

    public static boolean delete(String[] ids){
    Connection conn=DBConnection.getConn();
    Statement pst=null;
    try{
    conn.setAutoCommit(false);// 更改JDBC事务的默认提交方式
     StringBuffer sql=new StringBuffer();
     StringBuffer rsql=new StringBuffer();
     sql.append("delete from SysFun where id in ( "+ids[0]);
     rsql.append("delete from Raul where sysFunid in ( "+ids[0]);
     pst=conn.createStatement();

     if(ids!=null&&ids.length>0){
     
     // System.out.print(delValues.length);
     for(int i=1;i<ids.length;i++){//自动判断勾选时参数?的个数
      sql.append(","+ids[i]);  
      
     }}
     sql.append(")");
     rsql.append(")");

     pst.executeUpdate(rsql.toString());
     pst.executeUpdate(sql.toString());
     pst=conn.createStatement();


    conn.commit();//提交JDBC事务
    conn.setAutoCommit(true);// 恢复JDBC事务的默认提交方式
    return true;
    }catch (Exception e) {
     try {
    conn.rollback();//回滚JDBC事务
    } catch (SQLException e1) {

    e1.printStackTrace();
    }
     e.printStackTrace();
     return false;
    }
    finally{
    if(pst!=null){
    try {
    pst.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    DBConnection.Close(conn);
    }



    }看看吧!我以前做的了!不知道行不!
      

  5.   

    STATEMENT多个就多个,我还10几个一齐来用
      

  6.   

    1.以前用JDBC处理多个Statement的时候,总会因为异常而无法完美的解决Statement正常关闭,今天无意看到老外写了以下一段代码,和大家分享一下:   
    2.  
    3.    
    4.  
    5.Java代码    
    6.private PreparedStatement psStmt1;      
    7.private PreparedStatement psStmt2;      
    8.private PreparedStatement psStmt3;      
    9.       
    10.........................      
    11.........................      
    12.     
    13./*  关闭代码  */     
    14.public void cleanup() throws SQLException {      
    15.  SQLException exception = null;      
    16.  if (psStmt1 != null) {      
    17.    try {      
    18.      psStmt1.close();      
    19.    } catch (SQLException e) {      
    20.      exception = e;      
    21.    } finally {      
    22.      psStmt1 = null;      
    23.    }      
    24.  }      
    25.       
    26.  if (psStmt2 != null) {      
    27.    try {      
    28.      psStmt2.close();      
    29.    } catch (SQLException e) {      
    30.      if (exception != null) e.setNextException(exception);      
    31.      exception = e;      
    32.    } finally {      
    33.      psStmt2 = null;      
    34.    }      
    35.  }      
    36.       
    37.  if (psStmt3 != null) {      
    38.    try {      
    39.      psStmt3.close();      
    40.    } catch (SQLException e) {      
    41.      if (exception != null) e.setNextException(exception);      
    42.      exception = e;      
    43.    } finally {      
    44.      psStmt3 = null;      
    45.    }      
    46.  }      
    47.       
    48.  if (exception != null) {      
    49.    throw exception;      
    50.  }      
    51.}     
    52.  
    53.private PreparedStatement psStmt1;   
    54.private PreparedStatement psStmt2;   
    55.private PreparedStatement psStmt3;   
    56.    
    57.........................   
    58.........................   
    59.  
    60./*  关闭代码  */  
    61.public void cleanup() throws SQLException {   
    62.  SQLException exception = null;   
    63.  if (psStmt1 != null) {   
    64.    try {   
    65.      psStmt1.close();   
    66.    } catch (SQLException e) {   
    67.      exception = e;   
    68.    } finally {   
    69.      psStmt1 = null;   
    70.    }   
    71.  }   
    72.    
    73.  if (psStmt2 != null) {   
    74.    try {   
    75.      psStmt2.close();   
    76.    } catch (SQLException e) {   
    77.      if (exception != null) e.setNextException(exception);   
    78.      exception = e;   
    79.    } finally {   
    80.      psStmt2 = null;   
    81.    }   
    82.  }   
    83.    
    84.  if (psStmt3 != null) {   
    85.    try {   
    86.      psStmt3.close();   
    87.    } catch (SQLException e) {   
    88.      if (exception != null) e.setNextException(exception);   
    89.      exception = e;   
    90.    } finally {   
    91.      psStmt3 = null;   
    92.    }   
    93.  }   
    94.    
    95.  if (exception != null) {   
    96.    throw exception;   
    97.  }   
    98.}   
    欣赏一下吧
      

  7.   

    Statement用于在已经建立的连接的基础上向数据库发送SQL语句的对象。
    你的SQL已经执行完成了 这时候关闭它应该问题不大
    不过我看你都是在finally里面close的 finally是最后执行的一般都是在这里关闭你的代码里面怎么那么多try嵌套··
      

  8.   

    close后能关闭吗?没有人回答,还有啊,4#的那2个STATEMENT中有一个是不是没有关闭啊
      

  9.   

    7#的大哥,你的意思,是statement已经提交了SQL,所以关闭后,对con的COMMIT操作和rollback操作没有影响了,我的嵌套是为了保证关闭
      

  10.   

    这样就能保证三个statement都正常关闭吗?还有请不要把代码重复粘贴……
      

  11.   

    con 想当于一个telnet到一个远程主机上数据库服务器
    Statement 相当语句
    这是面向对象的思想,没有难的