数据库连接池的创建与运用

//数据库连接池,用集合保存一大批connection,用户-servlet-dao-db
//-->改为用户-servlet-dao-connectionpool--db
public class jdbcpool implements DataSource {

	//连接池里面先保存多个连接,供dao取出连接,要增删改查连接用linkedlist
	private static LinkedList<Connection>list=new LinkedList<Connection>();

	static{
		Properties p=new Properties();
		InputStream in=jdbcpool.class.getClassLoader().getResourceAsStream("db.properties");
		try {
			p.load(in);
			for(int i=0;i<10;i++)//为数据库创建10个连接,放到连接池
			{
				Class.forName(p.getProperty("driver"));
				Connection con=DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"), p.getProperty("password"));
				list.add(con);
			}
		}  catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public Connection getConnection() throws SQLException {

		//从连接池里面取出数据
		if(list.size()<=0)
		{
			throw new RuntimeException();
		}
		Connection con=list.removeFirst();
		//每次都移除第一个连接,连接池里减少一个,相当于取出了一个连接

		//conn.Close();这样释放资源的话只会释放到数据库中,不会释放到连接池中
		//相当于我们可以增强con.close()方法,当close()的时候,归还到连接池中去

		MyConnection my=new MyConnection(con);

		return my;
	}
	//增强方法有3种方法
	//1:写一个connction子类,覆盖close()方法,增强close()方法
	//2:用包装设计模式
	//3:动态代理

	//使用包装设计模式增强才,close()方法,包装设计模式步奏
		/*
		 * 1:定义一个类,实现与增强相同的接口
		 * 2:在类中定义一个变量,记住被增强对象
		 * 3:定义一个构造函数,接受被增强对象
		 * 4:覆盖想增强方法
		 * 5:对于不想增强的方法,直接调用目标对象的方法
		 */
		class MyConnection implements Connection
		{
			private Connection con;
			public MyConnection(Connection con)
			{
				this.con=con;
			}
			@Override
			public void close() throws SQLException {

				list.add(con);//释放数据库资源的时候,直接释放到连接池中去

			}

		@Override
		public <T> T unwrap(Class<T> iface) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean isWrapperFor(Class<?> iface) throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public Statement createStatement() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql) throws SQLException {
			// TODO Auto-generated method stub
			return this.con.prepareStatement(sql);
		}

		@Override
		public CallableStatement prepareCall(String sql) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String nativeSQL(String sql) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void setAutoCommit(boolean autoCommit) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean getAutoCommit() throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void commit() throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public void rollback() throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean isClosed() throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public DatabaseMetaData getMetaData() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void setReadOnly(boolean readOnly) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean isReadOnly() throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void setCatalog(String catalog) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public String getCatalog() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void setTransactionIsolation(int level) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public int getTransactionIsolation() throws SQLException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public SQLWarning getWarnings() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void clearWarnings() throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public Statement createStatement(int resultSetType, int resultSetConcurrency)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql, int resultSetType,
				int resultSetConcurrency) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public CallableStatement prepareCall(String sql, int resultSetType,
				int resultSetConcurrency) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Map<String, Class<?>> getTypeMap() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public void setHoldability(int holdability) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public int getHoldability() throws SQLException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Savepoint setSavepoint() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Savepoint setSavepoint(String name) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void rollback(Savepoint savepoint) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public void releaseSavepoint(Savepoint savepoint) throws SQLException {
			// TODO Auto-generated method stub

		}

		@Override
		public Statement createStatement(int resultSetType,
				int resultSetConcurrency, int resultSetHoldability)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql, int resultSetType,
				int resultSetConcurrency, int resultSetHoldability)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public CallableStatement prepareCall(String sql, int resultSetType,
				int resultSetConcurrency, int resultSetHoldability)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public PreparedStatement prepareStatement(String sql, String[] columnNames)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Clob createClob() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Blob createBlob() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public NClob createNClob() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public SQLXML createSQLXML() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean isValid(int timeout) throws SQLException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void setClientInfo(String name, String value)
				throws SQLClientInfoException {
			// TODO Auto-generated method stub

		}

		@Override
		public void setClientInfo(Properties properties)
				throws SQLClientInfoException {
			// TODO Auto-generated method stub

		}

		@Override
		public String getClientInfo(String name) throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Properties getClientInfo() throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Array createArrayOf(String typeName, Object[] elements)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Struct createStruct(String typeName, Object[] attributes)
				throws SQLException {
			// TODO Auto-generated method stub
			return null;
		}

		}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}
}

  

package com.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

public class insertData {

	@Test
	public void testInsert() throws SQLException
	{
		Connection con=null;
		try{
		jdbcpool p=new jdbcpool();
		con=p.getConnection();
		String sql="insert into login value(‘jack‘,‘456‘)";
		PreparedStatement ps=con.prepareStatement(sql);
		int i=ps.executeUpdate();
		if(i>0)
		{
			System.out.println("insert success");
		}
		}
		catch (Exception e) {
		}
		finally{
			con.close();//释放资源到连接池
		}

	}
}

  

时间: 2024-12-15 01:53:10

数据库连接池的创建与运用的相关文章

c3p0数据库连接池使用--创建JDBCTools 公共类

package com.atguigu.jdbc; import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statemen

数据库连接池的创建

德鲁伊连接池的创建                                              //获取druid.properties配置文件的路径,(DruidUtils)是"druid.properties"文件的兄弟路径,getClassLoader()获取的是<他们>的运行时路径 InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("

java数据库连接池技术简单使用

JDBCDemo.java: package com.itheima.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.itheima.pool.MyPool; public class JDBCDemo { public static void main(String[]

【转】JDBC学习笔记(8)——数据库连接池(dbcp&amp;C3P0)

转自:http://www.cnblogs.com/ysw-go/ JDBC数据库连接池的必要性 一.在使用开发基于数据库的web程序时,传统的模式基本是按一下步骤: 1)在主程序(如servlet/beans)中建立数据库连接 2)进行sql操作 3)断开数据库连接 二.这种模式开发,存在的问题: 1)普通的JDBC数据库连接使用DriverManager来获取,每次向数据库建立连接的时候都要将Connection加载进内存中,再验证用户名和密码(得花费0.05s~1s的时间).需要数据库连接

【Java EE 学习第15天】【自定义数据库连接池之动态代理的使用】

一.动态代理的作用 使用动态代理可以拦截一个对象某个方法的执行,并执行自定义的方法,其本质是反射 优点:灵活 缺点:由于其本质是反射,所以执行速度相对要慢一些 二.数据库连接池设计思想 1.为什么要使用数据库连接池:创建Connection对象的过程是非常耗时的,为了保证Connection可以重用,应该对Connection进行管理. 2.设计要求: (1)连接池能够实现维护多个连接,必须要保证每一个线程获取到的是不同的Connection对象. (2)提供一个方法能够回收连接. 3.最基本的

理解数据库连接池底层原理之手写实现

第一,数据库连接池中存放的就是数据库操作管道,不仅仅是存放,而且应该是管理这些管道: 第二,应该提供外部配置文件去初始化数据库连接池: 第三,如果一个数据库操作管道已经被占用,那么其他请求是否应该得到这个管道,也就是说我们要考虑多线程并发下,管道的分配问题: 第四,如果做到管道的复用?放回池子中,标示可用,并不是真正的关闭管道: IMyPool是一个接口,对外提供数据库连接池的基本服务,比如得到一个数据库操作管道. MyDefaultPool是IMyPool的实现. MyPooledConnec

DBCP数据库连接池-方式2手动创建

DBCPUtils.java package com.itheima.b_dbcp; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class DBCPUtils { private static BasicDataSource dataSource ; static{ try { // 手动创建连接池 dataSou

C3P0数据库连接池-方式1手动创建

C3P0是常用的数据连接池技术(第三方提供) 也是基于核心类DataSource. DBCPUtils.java package com.itheima.b_dbcp; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class DBCPUtils { private static BasicDataSource dat

7.创建数据库连接池

1.直接上代码,不废话,创建一个一样名字的类,复制粘贴就OK public class ConnectionPool { private String jdbcDriver = ""; // 数据库驱动 private String dbUrl = ""; // 数据 URL private String dbUsername = ""; // 数据库用户名 private String dbPassword = ""; //