Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
没事查看很早以前的备份文件,发现7年前写的一个连库类,想起了当年的点点滴滴,原来代码也是有生命的。


package com.itgenius.netoa.admin;

import java.sql.*;
import javax.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;

import java.util.List;

public class EJBDAO implements ApplicationUtil {
  
private  Connection conn=null;
  
private Statement st=null;
  
private PreparedStatement pst=null;
  
private CallableStatement cs=null;
  
private ResultSet rs=null;
  
private DataSource ds=null;
  
private boolean isCorrect=false;

  
public EJBDAO() {
  }
  
public void getConnection(){
    
try{
      Context ctx 
= new InitialContext();
      ds
=(DataSource)ctx.lookup(dsJndi);
      conn
=ds.getConnection();
    }
catch (NamingException ex) {
      ex.printStackTrace();
    }
catch(SQLException e){
      e.printStackTrace();
    }
  }
  
public void getConnection(String dburl,String dbdriver,String dbusername,String dbpasswd){
  }
  
public void getStatement(){
    
try{
      getConnection();
      st 
= conn.createStatement();
    }
catch (SQLException ex) {
      ex.printStackTrace();
    }
  }
  
//得到预备状态通道
  public void getPStatement(String sql){
    
try{
      getConnection();
      pst 
= conn.prepareStatement(sql);
    }
catch (SQLException ex) {
    }
  }
  
//执行查询得到结果集
  public ResultSet getResultSet(String sql){
    
try{
       getStatement();
       rs
=st.executeQuery(sql);
    }
catch(SQLException ex) {
    }
finally{
       
return rs;
    }
  }
  
//给预备状态通道中的sql的变量付值,然后执行
  public boolean executePstatement(List list){
     isCorrect
=false;
     
try{
          
for(int i = 0; i < list.size(); i++){
            pst.setString((i 
+ 1), list.get(i).toString());
          }
          pst.executeUpdate();
          isCorrect
=true;
      }
catch (SQLException ex) {
      }
finally{
         
return isCorrect;
      }
  }
  
//执行新增、修改、删除
  public boolean doUpdate(String sql){
    isCorrect
=false;
    
try{
      getStatement();
      st.executeUpdate(sql);
      isCorrect
=true;
    }
catch (SQLException ex){
       ex.printStackTrace();
    }
finally{
      
return isCorrect;
    }
  }
  
//执行存储过程
  public boolean doProcure(String pname,List list){
      isCorrect
=false;
    
try{
        getConnection();
       cs
=conn.prepareCall("{call "+pname+"}");
       
for(int i = 0; i < list.size(); i++)
       {
           cs.setString((i 
+ 1), list.get(i).toString());
       }
       cs.execute();
       isCorrect
=true;
    }
catch (SQLException ex) {
    }
finally{
      
return isCorrect;
    }
  }
  
public void addBatch(List list){
    
try{
      
for(int i=0;i<list.size();i++){
        st.addBatch(list.get(i).toString());
      }
    }
catch(Exception e){
    }
  }
  
public boolean ExecuteBatch(){
     isCorrect
=false;
     
try{
       st.executeBatch();
       isCorrect
=true;
     }
catch(Exception e){
        e.printStackTrace();
     }
finally{
        Close();
        
return isCorrect;
     }
  }
  
//关闭连接
  public void Close(){
      
try{
        
if(rs!=null)
        {
          rs.close();
        }
        
if(st!=null){
          st.close();
        }
        
if(pst!=null)
        {
          pst.close();
        }
        
if(cs!=null)
          cs.close();
        
if(conn!=null)
        conn.close();
      }
catch (SQLException ex) {
      }
  }

}

posted on 2013-03-01 09:52 礼物 阅读(556) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。

网站导航: