求jdbc connection,statement,resultset封装及存储callablestatement调用

解决方案 »

  1.   

    DBAccess  dba = null;

    try{
    CallableStatement proc = null; 
    dba = new DBAccess();
         proc=dba.getConn().prepareCall("{call import_t_emp_assess_1(?,?)}");
    // proc = dba.getConn().prepareCall("{call import_emp_info(?)}");
    proc.setInt(1,org_id);
    proc.registerOutParameter(2,Types.TINYINT);
    proc.execute();
    int temp1=proc.getInt(2);
    if(temp1==0){System.out.println("1:The result status is "+temp1+" success!");}
    else{System.out.println("1failure!");}
    }
         catch(Exception e){
    //     dba.rollback();
         throw new Exception("发生错误"+e);
         }
         finally {
        
         if( dba != null){
         dba.close();
         }
    }
      

  2.   

    package nedu.info.toolbean;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;public class ConnDB {
     private String url="jdbc:mysql://localhost:3306/db_info";
     private String name="root";
     private String pwd="123456"; 
     private Connection conn = null; 
     private Statement stmt=null;
     
     /* 通过构造方法加载数据库驱动 */
         public ConnDB(){
         try{
         Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception e){
      e.printStackTrace();
      System.out.println("加载数据库驱动失败!");
        }
         }     /* 获取数据库连接对象 */
         public Connection getConn(){
         try{
        conn=DriverManager.getConnection(url,name,pwd);
     }catch(Exception e){
     e.printStackTrace();
     System.out.println("获取数据库连接失败!");
     }
     return conn;
         }
         
         /* 获取Statement对象 */
         public void getStmt(){
    getConn();
        try {
      stmt=conn.createStatement();
        } catch (SQLException e) {
          e.printStackTrace();
          System.out.println("创建Statement对象失败!");
        }
         }
         
         /**
          * @功能 对数据库查询的操作
          * @参数 sql为要执行的SQL语句
          * @返回值 ResultSet型值
          */
        public ResultSet executequery(String sql) { 
       ResultSet rs = null; 
           try{
         getStmt();
         try { 
            rs = stmt.executeQuery(sql); 
         } catch (Exception e) { 
            e.printStackTrace(); 
            System.out.println("查询数据库失败!");
         } 
       } catch (Exception e) { 
       e.printStackTrace(); 
      } 
       return rs; 
     }      /**
          * @功能 对数据库的增加、修改和删除的操作
          * @参数 sql为要执行的SQL语句
          * @返回值 boolean型值
          */
          public boolean executeupdate(String sql) { 
          boolean rs=false;
          try { 
         getStmt();
            int rtn = stmt.executeUpdate(sql);
            if(rtn>0){
        rs=true;
            }else{
          rs=false;
          }
          } catch (Exception e) { 
             e.printStackTrace(); 
             rs=false;
             System.out.println("更新数据库失败!");
          } 
          return rs; 
      }       /* 关闭数据库的操作 */
          public void closed() {
           if(stmt!=null)
       try {
       stmt.close();
       } catch (SQLException e) {
       e.printStackTrace();
       System.out.println("关闭stm对象失败!");
       }
           if(conn!=null)
       try {
       conn.close();
       } catch (SQLException e) {
       e.printStackTrace();
       System.out.println("关闭con对象失败!");
       }
          }
    }这个怎么样,不行,还有别的!