当前位置 : 主页 > 编程语言 > c++ >

java oracle数据库连接池实例

来源:互联网 收集:自由互联 发布时间:2021-06-30
DBTools.java package com.web.Tools;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.List;import javax.naming.Context;import javax.
DBTools.java
package com.web.Tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
* @ClassName: DBTools
* @Description: 数据库连接工具类 
*
*/ 
public class DBTools {
	
	static Connection conn = null;
    private static DataSource ds = null;
    //在静态代码块中创建数据库连接池
    static{
        try{
             //初始化JNDI
            Context initCtx = new InitialContext();
            //得到JNDI容器,从JNDI容器中检索name为的java:comp/env/为前缀的数据源,通过lookup查找
            ds = (DataSource) initCtx.lookup("java:comp/env/aaa");
           
        }catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    /**
    * @Method: getConnection
    * @Description: 从数据源中获取数据库连接
    * @return Connection
    * @throws SQLException
    */ 
    public static Connection getConnection() throws SQLException{
        //从数据源中获取数据库连接
        return ds.getConnection();
    }
    
    /**
    * @Method: release
    * @Description: 释放资源,
    * 释放的资源包括Connection数据库连接对象,负责执行SQL命令的Statement对象,存储查询结果的ResultSet对象
    * @param conn
    * @param st
    * @param rs
    */ 
    public static void release(Connection conn,Statement st,ResultSet rs){
        if(rs!=null){
            try{
                //关闭存储查询结果的ResultSet对象
                rs.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(st!=null){
            try{
                //关闭负责执行SQL命令的Statement对象
                st.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        if(conn!=null){
            try{
                //将Connection连接对象还给数据库连接池
                conn.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static int executeUpdate(String sql,List list) {
    	int rs = 0;
    	try {
			conn = getConnection();
			PreparedStatement st = conn.prepareStatement(sql);
			for (int i = 0; i < list.size(); i++) {
				st.setObject(i+1, list.get(i));
			}
			rs = st.executeUpdate();
			return rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;	
    }
    
    public static ResultSet getResultSet(String sql,List list) { ResultSet rs = null; try { conn = getConnection(); PreparedStatement st = conn.prepareStatement(sql); for (int i = 0; i < list.size(); i++) { st.setObject(i+1, list.get(i)); } return st.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public static ResultSet getResultSet(String sql) { ResultSet rs = null; try { conn = getConnection(); PreparedStatement st = conn.prepareStatement(sql); return st.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } } 
网友评论