免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 787 | 回复: 0
打印 上一主题 下一主题

读取连接池的类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-06 20:23 |只看该作者 |倒序浏览
(一)DbPool.java:
package com.db;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
public class DbPool {
    private static String strError = "";
    private static Object poolLock = new Object();
    private static DataSource connectionPool;
     
    private Connection conn = null;
   
    public Connection getConnection() {
        synchronized (poolLock) {
            if (connectionPool == null) {
                try {
                    InitialContext ctx = new InitialContext();
                    connectionPool = (DataSource) ctx.lookup("java:comp/env/jdbc/DbPool");
                }
                catch (Exception e) {
                    strError = "DbPool[0]:" + e.getMessage();
                    //System.out.println( e.getMessage() );
                    return null;
                }
            }
        }//synchronized
        
        try {
            conn = connectionPool.getConnection();
            conn.setAutoCommit(false);
        }catch (SQLException e) {
            strError = "DbPool[1]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            return null;
        }
        
        return conn;
    }//end getConnection()
   
    public String getError() {
        return strError;
    }
}

(二)SQLExecute.java:
package com.db;
import java.sql.*;
public class SQLExecute {
    private String strError = "";
   
    DbPool dbPool = new DbPool();
    private Connection conn = dbPool.getConnection();
  
    //返回查询(select)结果方法
    public ResultSet executeQuery(String sql){
        if ( conn == null ){//没有拿到连接
            strError = dbPool.getError();
            return null;
        }
   
        ResultSet rs = null;
        Statement stmt = null;
        try {
            //允许记录集指针跳转 rs.last(); rs.previous();
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
        }catch (SQLException e) {
            strError = "SQLExecute[0]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            return null;
        }
        return rs;
    }
  
    //返回执行(insert,update,delete)结果方法
    public int executeUpdate(String sql) {
        int i=0;
        try {
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            i = stmt.executeUpdate(sql);
            conn.commit();
        }catch (SQLException e) {
            strError = "SQLExecute[1]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            i=-1;
        }
        return i;
    }
  
    //事务处理方法
    public int executeUpdatePre(String sql) {
        int i=0;
        try {
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            i = stmt.executeUpdate(sql);
        }catch (SQLException e) {
            strError = "SQLExecute[2]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            i=-1;
        }
        return i;
    }
  
    //事务提交
    public boolean Commit() {
        boolean b = false;
        try {
            if(conn != null) conn.commit();
            b = true;
        }catch (SQLException e) {
            strError = "SQLExecute[3]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            return false;
        }
        return b;
    }
  
    //关闭数据连接方法
    public boolean closeConn() {
        boolean b = false;
        try {
            if(conn != null) conn.close();
        }catch (SQLException e) {
            strError = "SQLExecute[4]:" + e.getMessage();
            //System.out.println( e.getMessage() );
            return false;
        }
        return b;
    }
   
    public String getError() {
        return strError;
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4404/showart_166066.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP