Configuration and use of database connection pool under Tomcat

Experimental environment: Eclipse Neon.3 (4.6.3), MySQL, Tomcat 9.0

1. Create a new context.xml file under META-INF under the web application to configure the data source.

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource name="DBPool"
	type="javax.sql.DataSource" 
	auth="Container"
	driverClassName="com.mysql.jdbc.Driver" 
	url="jdbc:mysql://localhost:3306/newsdb" 
	username="root"
	password="root" 
	maxActive="5" 
	maxIdle="2" 
	maxWait="6000" />
</Context>

Second, use JNDI to access the database connection pool

package com.drathin.db;

import java.sql.*;

import javax.naming.*;
import javax.sql.DataSource;


public class Dbpool {

	protected static Statement s = null;
	protected static ResultSet rs = null;
	protected static Connection conn = null;
	
	public static Connection getConnection()
	{
		try
		{
			//Context是javax.name包中的一个接口,用于查找数据库连接池的配置文件
			Context ctx = new InitialContext();  //向上转型
			ctx = (Context) ctx.lookup("java:comp/env");
			
			DataSource ds = (DataSource) ctx.lookup("DBPool");
			conn = ds.getConnection();
		}catch(Exception e)
		{
			e.printStackTrace();
			//System.out.println("FAil");
		}
		return conn;
	}

	public static Statement getS() {
		return s;
	}

	public static void setS(Statement s) {
		Dbpool.s = s;
	}

	public static ResultSet getRs() {
		return rs;
	}

	public static void setRs(ResultSet rs) {
		Dbpool.rs = rs;
	}

	public static Connection getConn() {
		return conn;
	}

	public static void setConn(Connection conn) {
		Dbpool.conn = conn;
	}

	

}

3. Test with JSP script

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="com.drathin.db.Dbpool"
    import="java.sql.*"
    %>
<!DOCTYPE html>
<html>
<head>

<title>Tomcat连接池测试</title>
</head>
<body>
<%
	Dbpool.setConn(Dbpool.getConnection());	

	
	try {
		Dbpool.setS(Dbpool.getConn().createStatement());
		Dbpool.setRs(Dbpool.getS().executeQuery("select * from user"));
		while(Dbpool.getRs().next()){
			out.print("  id:" + Dbpool.getRs().getInt(1));
			out.print(";  username:" + Dbpool.getRs().getString(2));
			out.print(";  password:" + Dbpool.getRs().getString(3));
			out.print("\n");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
%>

</body>
</html>

Note: Do not use java applications for the test program, because this is the database connection pool of Tomcat. If it is run under eclipse, it will cause Tomcat to shut down, and the operation will throw an exception. You can also use servlet to implement it, and then run it under Tomcat .

package com.drathin.db;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/DBPoolServlet")
public class DBPoolServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DBPoolServlet() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
		Dbpool.setConn(Dbpool.getConnection());	

		try {
			Dbpool.setS(Dbpool.getConn().createStatement());
			Dbpool.setRs(Dbpool.getS().executeQuery("select * from user"));
			while(Dbpool.getRs().next()){
				System.out.print("  id:" + Dbpool.getRs().getInt(1));
				System.out.print(";  username:" + Dbpool.getRs().getString(2));
				System.out.print(";  password:" + Dbpool.getRs().getString(3));
				System.out.print("\n");
			}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

 

Guess you like

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