Apache-based DBCP to establish a Web-independent database connection pool configuration

When our web application runs independently and does not require the support of a web server, we want to apply a database connection pool, and we can use an independent database connection pool solution. Apache's DBCP is one of them. The configuration steps are as follows:

Step 1. Download the three necessary jar packages

commons-dbcp2-2.2.0.jar
commons-logging-1.2.jar
commons-pool2-2.5.0.jar

Step 2. Create a DBCP-based database connection pool public class

package com.drathin.dbpool;

import java.sql.*;
import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.tomcat.jdbc.pool.DataSource;



public class DbcpPool {
	private static BasicDataSource dataSource = null;
	
	public DbcpPool(){
		
	}
	
	//初始化数据库连接池
	public static void init(){
		if(dataSource != null){
			try{
				dataSource.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			dataSource = null;
		}
	
	
		//使用properties对象定义数据库连接池信息
		try{
			Properties p = new Properties();
			p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
			p.setProperty("url","jdbc:mysql://localhost:3306/maiba");
			p.setProperty("username","root");
			p.setProperty("password","root");
			p.setProperty("maxActive","30");
			p.setProperty("maxIdle","10");
			p.setProperty("maxWait","1000");
			p.setProperty("removeAbandoned","false");
			p.setProperty("removeAbandonedTimeout","120");
			p.setProperty("testOnBorrow","true");
			p.setProperty("logAbandoned","true");
			dataSource = (BasicDataSource)BasicDataSourceFactory.createDataSource(p);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//从连接池中获取连接
	public static synchronized Connection getConnection() throws SQLException{
		if(dataSource == null){
			init();
		}
		Connection conn = null;
		if(dataSource != null){
			conn = dataSource.getConnection();
		}
		return conn;
	}
}

Step 3. Test the page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="com.drathin.dbpool.*"
    import="java.sql.*"
    %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>独立于web服务器的数据库连接池</title>
</head>
<body>
<table  border="0" cellspacing="1" cellpadding="4" bgcolor="#7fccff"  align="center"  width="60%">
<%
	Connection conn = DbcpPool.getConnection();
	Statement stmt = conn.createStatement();
	ResultSet rs = stmt.executeQuery("select * from user");
	while(rs.next()){
		%><tr bgcolor="#ffffff" width="600px"><td>I D</td><td><%=rs.getInt("id") %></td></tr><%
				%><tr bgcolor="#ffffff" ><td>账 户</td><td><%=rs.getString("account") %></td></tr><%
				%><tr bgcolor="#ffffff" ><td>密 码</td><td><%=rs.getString("password") %></td></tr><%
				%><tr bgcolor="#ffffff" ><td>昵 称</td><td><%=rs.getString("user_name") %></td></tr><%
				%><tr bgcolor="#ffffff" ><td>邮 箱</td><td><%=rs.getString("email") %></td></tr><%

	} 
	conn.close();
%>
</table>

</body>
</html>

 

Attached database:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'drathin', '123456', 'Drathin', '[email protected]');

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325092780&siteId=291194637