自定义的数据库连接池(C3P0)JAVA版

本文详细介绍了一种自定义的Java数据库连接池实现方法,通过创建连接池管理器类DBManager,实现了数据库连接的初始化、获取和释放,有效提高了数据库访问效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前有写java通过c3p0连接池与数据库进行连接。但是之前写的和普通的连接没啥子区别的样子,完全没有起到池的作用。我们知道,池应该可以理解为一种对数据库的一种直接映射,但是又有不同。池的作用,其实就是一些小的数据库的集群。我们所要的,都在这个池子里。从而避免了对数据库的直接操作,也就是这个池就是我们的数据库。

因为原来写的没有起到池的作用,只起到了一个中间者的作用,所以这次再来一次好了。自定义一个数据库连接池,大家可以自行修改的。

package com.sun.db;
import java.sql.*;
/**
 * @author SunY
 * 自定义的数据库连接池
 * */
public class JDBCUtil {

	/**
	 * The load driver
	 * */
	public static void loadDriver() throws ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
	}

	/**
	 * @param 
	 * @return connection
	 * */
	public static Connection getconnection() throws SQLException {
		try {
			loadDriver();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
	}
	
	/**
	 * @param connection 
	 * @param resultset 
	 * @param statement 
	 * */
	public static void release(Connection connection , Statement statement , ResultSet resultset) throws SQLException {
		if(null != connection) {
			connection.close();
			connection = null ;
		}
		if(null != statement) {
			statement.close();
			statement = null ;
		}
		if(null != resultset) {
			resultset.close();
			resultset = null ;
		}
	}
	
	/**
	 * @param connection 
	 * @param statement 
	 * */
		public static void release(Connection connection , Statement statement ) throws SQLException {
			if(null != connection) {
				connection.close();
				connection = null ;
			}
			if(null != statement) {
				statement.close();
				statement = null ;
			}
		}
}
package com.sun.db;

import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.sql.DataSource;

/**
 * @author SunY
 * */
public class DBManager implements DataSource{
	List<Connection> conns = new ArrayList<>();
	private final static long INIT_SIZE = 5 ;// 数据库连接池的初始大小
	

	public DBManager() {
		super();
		for (int i = 0; i < INIT_SIZE; i++) {
			// 得到一个连接
			try {
				Connection connection = JDBCUtil.getconnection();
                        conns.add(connection);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public Connection getConnection() throws SQLException {
		// 当连接池满了或者为空时
		if(conns.size() == 0 || conns.isEmpty()) {
			for(int i = 0 ; i < 5 ; i ++ ) {
				conns.add(JDBCUtil.getconnection());
			}
		}
		return conns.remove(0);
	}
	/**
	 * 
	 *  将连接返回连接池
	 * @param conn
	 * */
	public void addBack(Connection conn) {
		conns.add(conn);
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {

		return null;
	}
	
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return null;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return 0;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {

		return null;
	}

	@Override
	public void setLogWriter(PrintWriter arg0) throws SQLException {

		
	}

	@Override
	public void setLoginTimeout(int arg0) throws SQLException {
		
	}

	@Override
	public boolean isWrapperFor(Class<?> arg0) throws SQLException {

		return false;
	}

	@Override
	public <T> T unwrap(Class<T> arg0) throws SQLException {

		return null;
	}



}
package com.sun.operate;

import java.sql.*;
import com.sun.db.DBManager;
/**
 * @author SunY
 * */
public class Test {


	public static void main(String[] args) {
		DBManager db = new DBManager();
		PreparedStatement statement = null ;
		Connection connection =null ;
		try {
			connection = db.getConnection();
			String sql = "insert into user(id,username,password) values (?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setInt(1,5);
			statement.setString(2, "sunyang");
			statement.setString(3, "sun18895378485");
			int rs = statement.executeUpdate();
			if(rs!=0) {
				System.out.println("insert successful");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(null != connection) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null ;
			}
			db.addBack(connection);
			if(null != statement) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				statement = null ;
			}
		}
		
		

	}

}

到此便结束了。我还是相信虽然解释很少,还是看得懂的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值