实验环境:Eclipse Neon.3 (4.6.3)、MySQL、Tomcat 9.0
一、在web应用下的META-INF下新建context.xml文件,配置数据源。
1<?xml version="1.0" encoding="UTF-8"?> 2<Context> 3 <Resource name="DBPool" 4 type="javax.sql.DataSource" 5 auth="Container" 6 driverClassName="com.mysql.jdbc.Driver" 7 url="jdbc:mysql://localhost:3306/newsdb" 8 username="root" 9 password="root" 10 maxActive="5" 11 maxIdle="2" 12 maxWait="6000" /> 13</Context>
二、使用JNDI访问数据库连接池
1package com.drathin.db; 2 3import java.sql.*; 4 5import javax.naming.*; 6import javax.sql.DataSource; 7 8 9public class Dbpool { 10 11 protected static Statement s = null; 12 protected static ResultSet rs = null; 13 protected static Connection conn = null; 14 15 public static Connection getConnection() 16 { 17 try 18 { 19 //Context是javax.name包中的一个接口,用于查找数据库连接池的配置文件 20 Context ctx = new InitialContext(); //向上转型 21 ctx = (Context) ctx.lookup("java:comp/env"); 22 23 DataSource ds = (DataSource) ctx.lookup("DBPool"); 24 conn = ds.getConnection(); 25 }catch(Exception e) 26 { 27 e.printStackTrace(); 28 //System.out.println("FAil"); 29 } 30 return conn; 31 } 32 33 public static Statement getS() { 34 return s; 35 } 36 37 public static void setS(Statement s) { 38 Dbpool.s = s; 39 } 40 41 public static ResultSet getRs() { 42 return rs; 43 } 44 45 public static void setRs(ResultSet rs) { 46 Dbpool.rs = rs; 47 } 48 49 public static Connection getConn() { 50 return conn; 51 } 52 53 public static void setConn(Connection conn) { 54 Dbpool.conn = conn; 55 } 56 57 58 59}
三、用JSP脚本测试
1<%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" 3 import="com.drathin.db.Dbpool" 4 import="java.sql.*" 5 %> 6<!DOCTYPE html> 7<html> 8<head> 9 10<title>Tomcat连接池测试</title> 11</head> 12<body> 13<% 14 Dbpool.setConn(Dbpool.getConnection()); 15 16 17 try { 18 Dbpool.setS(Dbpool.getConn().createStatement()); 19 Dbpool.setRs(Dbpool.getS().executeQuery("select * from user")); 20 while(Dbpool.getRs().next()){ 21 out.print(" id:" + Dbpool.getRs().getInt(1)); 22 out.print("; username:" + Dbpool.getRs().getString(2)); 23 out.print("; password:" + Dbpool.getRs().getString(3)); 24 out.print("\n"); 25 } 26 } catch (SQLException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30%> 31 32</body> 33</html>
注意:测试程序切勿用java应用程序,因为这是Tomcat的数据库连接池,如果在eclipse下run运行,会导致Tomcat关闭,而运行抛出异常,也可以用servlet去实现,然后运行在Tomcat下。
1package com.drathin.db; 2 3import java.io.IOException; 4import java.sql.SQLException; 5 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12 13@WebServlet("/DBPoolServlet") 14public class DBPoolServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 public DBPoolServlet() { 18 super(); 19 20 } 21 22 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 24 response.getWriter().append("Served at: ").append(request.getContextPath()); 25 Dbpool.setConn(Dbpool.getConnection()); 26 27 try { 28 Dbpool.setS(Dbpool.getConn().createStatement()); 29 Dbpool.setRs(Dbpool.getS().executeQuery("select * from user")); 30 while(Dbpool.getRs().next()){ 31 System.out.print(" id:" + Dbpool.getRs().getInt(1)); 32 System.out.print("; username:" + Dbpool.getRs().getString(2)); 33 System.out.print("; password:" + Dbpool.getRs().getString(3)); 34 System.out.print("\n"); 35 } 36 } catch (SQLException e) { 37 38 e.printStackTrace(); 39 } 40 } 41 42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 43 44 doGet(request, response); 45 } 46 47}