下载c3p0包:
下载地址:https://sourceforge.net/projects/c3p0/files/?source=navbar
解压后得到包:c3p0-0.9.2.jar,mchange-commons-java-0.2.11.jar
下载mysql包:
下载地址:http://download.csdn.net/download/u010802461/9579306
解压后得到包:mysql-connector-java-5.1.39-bin.jar(笔者这里没有是因为我将包放到Tomcat服务器的lib文件夹下)
将包都拷贝到项目lib下,注意外部mysql必须开启
在META-INF中新建context.xml
并且在context.xml中添加代码
<?xml version="1.0" encoding="UTF-8"?> <Context> <!-- 使用C3P0配置针对MySQL数据库的JNDI数据源 --> <Resource name="jdbc/mysql" auth="Container" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" driverClass="com.mysql.jdbc.Driver" idleConnectionTestPeriod="60" maxPoolSize="50" minPoolSize="2" acquireIncrement="2" user="root" password="root" jdbcUrl="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8"/> </Context>
context.xml的代码
在Web.xml配置文件中写代码
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Test1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
Web.xml代码
测试代码是否正常运行
package com.c3p0.jndi; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; @WebServlet("/Testc3p0") public class Testc3p0 extends HttpServlet { private static final long serialVersionUID = 1L; public Testc3p0() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ResultSet rs = null; try { //此类是执行命名操作的初始上下文。用于解析该 URL InitialContext ctx = new InitialContext(); //lookup检索配置。 DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); //获取连接 Connection con = ds.getConnection(); /*Statement stmt = con.createStatement(); rs = stmt.executeQuery("select count(*) as ant from user"); while(rs.next()){ if(rs!=null){ System.out.println(rs.getInt("ant")); } }*/ System.out.println(ds.getConnection()); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
时间: 2024-10-13 18:41:24