JDBC beginner (two): JDBC pool

    池:内存空间
 * 连接池:存放连接资源的一个容器
 * 
 * 为什么要用连接池:
 * 每次访问数据库都要创建一个新的连接,频繁的创建和关闭连接会耗费时间而且耗费资源,因此会导致程序执行的效率低下。

Import the use of c3p0 connection pool

  • 1. Import c3P0 development kit (jar package)
  • 2. Create a connection pool object in the program (container: a batch of connections will be stored inside)
  • 3. Set the basic information of connecting to the database (the full path of the driver class, the URL address of the connected library, the user name and password of the connected library)
  • Method 1: Write the connection parameters directly in the program, if there are changes in the future, you need to modify the program, which will bring a lot of unnecessary trouble
  • Method 2: Configure the basic information of connecting to the database in the c3p0.properties file, which is placed in the src root directory
  • Method 3: Configure the basic information of connecting to the database in the c3p0-config.xml file, which is placed in the src root directory
  • The file names in the second and third methods are fixed and cannot be changed, because the c3p0 program will automatically read these problems
  • 4. Get a connection object from the connection pool for use
  • 5. Return the connection to the connection pool

Step 1:
Insert picture description here
Download the c3p0-0.9.1.2.jar package on the official website, copy it to the lib (right click to create a new folder, name it lib) library, right click buildpath->add to the path;
Insert picture description here
then a bottle will appear in your library The jar package of the same name of the pattern

The second part creates a link: the code is as follows:

public class TestC3p0 {
    
    

	
	static ComboPooledDataSource pool=new ComboPooledDataSource();
//	//设置连接数据库的基本信息(方式一:不推荐使用)
//	static {
    
    
//		try {
    
    
//			pool.setDriverClass("com.mysql.jdbc.Driver");//设置驱动类
//			pool.setJdbcUrl("jdbc:mysql://localhost:3306/jt_db?characterEncoding=utf-8");
//			pool.setUser("root");
//			pool.setPassword("root");
//		} catch (PropertyVetoException e) {
    
    
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	@Test
	public void testFindAll() throws SQLException {
    
    
		//
		
		
		//注册驱动,并获取连接(创建一个新对象
		//Connection conn=jdbcUtil.getConn();
		//改为从连接池中获取一条连接
		Connection conn=pool.getConnection();
		
		
		//获取传输器
		String sql="select * from user";
		PreparedStatement ps=conn.prepareStatement(sql);
		//执行sql语句,返回执行接货
		ResultSet rs=ps.executeQuery();
		//输出结果
		System.out.println("id\t"+"username\t"+"password\t");
		while(rs.next()) {
    
    
			//System.out.println(rs.getInt("id")+"\t"+rs.getString("username")+"\t\t"+rs.getString("password"));
			System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t\t"+rs.getString(3));
		}
		//释放资源
		rs.close();
		ps.close();
		conn.close();
		/*如果当前这个连接对象,使我们自己创建的一个连接对象,调用conn.close方法默认就是将连接关闭(也就是释放掉)
		 * 如果当前这个连接对象是从连接池中获取过来的,在连接池返回这个连接对象时看,会改造连接对象的功能(方法)
		 * 将conn的close方法还链接到连接池中,而不是将连接关闭。
		 * 
		 */
	}
}

What we saw above is the first method to create a connection pool. The
second method is to create a new file in src and rename it to c3p0.prperties
so that it can be run directly because the program will automatically call it.

Guess you like

Origin blog.csdn.net/qq_45273552/article/details/107071223