package proctest;
import java.sql.*;
public class testBean {
 public testBean(){ }
 /**
  * 获取Conncetion对象
  * @return
  */
 public java.sql.Connection getConn(){
 try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    return java.sql.DriverManager.getConnection("jdbc:odbc:zhaohaha");
    }
  catch(Exception e){
    return null;
  }
 }/**
 *添加新记录
 * @return:0成功1失败
 */
 public int add(String name,String age){
     java.sql.Connection         _iconn  =  null;
     java.sql.CallableStatement  _ipst   = null;
  try{
    _iconn  = this.getConn();
    _ipst   = _iconn.prepareCall("{call insert_myproc(?,?)}");
    _ipst.setString(1,name);
    _ipst.setString(2,age);
    _ipst.executeUpdate();
    _ipst.close();
    _iconn.close();
    return 0;
  }
  catch(Exception e){
        try{
           if (_ipst!=null){_ipst.close();}
           if (_iconn!=null){_iconn.close();}
       }catch(Exception se){}
    return 1;
  } }
/**
 *删除数据记录
 * @param id
 * @return
 */
public int del(int id){
    java.sql.Connection           _iconn  = null;
    java.sql.CallableStatement    _ipst   = null;
   try{
      _iconn=this.getConn();
      _ipst=_iconn.prepareCall("{call del_myproc(?)}");
      _ipst.setInt(1,id);
      _ipst.executeUpdate();
      _ipst.close();
      _iconn.close();
      return 0;
   }
   catch(Exception e){
      try{
           if (_ipst!=null){_ipst.close();}
           if (_iconn!=null){_iconn.close();}
       }catch(Exception se){}
      return 1;
   }
}
/**
 *
 * @return
 */
public String select(){
   String rtn="<table><tr bgcolor=red><td>姓名</td><td>年龄</td></tr>";
   java.sql.Connection          _iconn  = null;
   java.sql.CallableStatement   _ipst   = null;
   java.sql.ResultSet           _irs    = null;
   try{
      _iconn  = this.getConn();
      _ipst   = _iconn.prepareCall("{call myproc()}");
      _irs    = _ipst.executeQuery();
   while (_irs.next()){
        rtn=rtn+"<tr bgcolor=yellow><td>"+_irs.getString("name")+"</td>";
        rtn=rtn+"<td>"+_irs.getString("name")+"</td></tr>";
   }
    rtn=rtn+"</table>";
    _irs.close();
    _iconn.close();
    return rtn;
   }
   catch(Exception e){
      try{
           if (_ipst!=null){_ipst.close();}
           if (_iconn!=null){_iconn.close();}
       }catch(Exception se){}
      return "";
   }
}
public int update(int id,String name,String age){
    java.sql.Connection         _iconn  = null;
    java.sql.CallableStatement  _ipst   = null;  try{
    _iconn  = this.getConn();
    _ipst   = _iconn.prepareCall("{update_myproc(?,?,?)}");
    _ipst.setInt(1,id);
    _ipst.setString(2,name);
    _ipst.setString(3,age);
    _ipst.executeUpdate();
    _ipst.close();
    _iconn.close();
    return 0;
     }
  catch(Exception e){
        try{
           if (_ipst!=null){_ipst.close();}
           if (_iconn!=null){_iconn.close();}
       }catch(Exception se){}
    return 1;
  }}}

解决方案 »

  1.   

    1.请注意commit();
    2.不能进行事务处理:因为每个方法都使用各自的connection
    3.释放connection资源建议用finally
    public int del(int id){
        java.sql.Connection           _iconn  = null;
        java.sql.CallableStatement    _ipst   = null;
       try{
          _iconn=this.getConn();
          _ipst=_iconn.prepareCall("{call del_myproc(?)}");
          _ipst.setInt(1,id);
          _ipst.executeUpdate();
          _ipst.close();
          _iconn.close();
          return 0;
       }
       catch(Exception e){
          try{
               if (_ipst!=null){_ipst.close();}
        //如果这句抛出异常,那么下面的关闭connection就不能执行,你的程序可能长时间的占用这个connection,
               if (_iconn!=null){_iconn.close();}
           }catch(Exception se){}
          return 1;
       }
    }
      

  2.   

    _iconn  
    命名不够规范吧
      

  3.   

    你的bean写的不够规范,也不完整。影响通用性。应该在加个setConn()
      

  4.   

    将几个语句组合起来可以形成简单的事务conn.setAutoCommit(false);//关闭自动提交
    Statement stat = conn.createStatement();
    stat.executeUpdate(sql1);
    stat.executeUpdate(sql2);
    stat.executeUpdate(sql3);
    ....
    ...
    conn.commit();出现错误就:
    conn.rollback();