Dao位于dao包
package cn.itcast.dao; import java.sql.Connection; import java.sql.SQLException; import cn.itcast.exception.DaoException; import cn.itcast.utils.JdbcUtils_Tomcat; public class Dao { public void add(){ Connection conn; try { conn = JdbcUtils_Tomcat.getConnection(); System.out.println(conn); //jdbc:mysql://localhost:3306/day16, UserName=root@localhost, MySQL-AB JDBC Driver System.out.println(conn.getClass().getName()); //org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper } catch (SQLException e) { throw new DaoException(); } } }
DaoException位于exception
package cn.itcast.exception; import java.io.PrintStream; import java.io.PrintWriter; public class DaoException extends RuntimeException { @Override public synchronized Throwable fillInStackTrace() { // TODO Auto-generated method stub return super.fillInStackTrace(); } @Override public Throwable getCause() { // TODO Auto-generated method stub return super.getCause(); } @Override public String getLocalizedMessage() { // TODO Auto-generated method stub return super.getLocalizedMessage(); } @Override public String getMessage() { // TODO Auto-generated method stub return super.getMessage(); } @Override public StackTraceElement[] getStackTrace() { // TODO Auto-generated method stub return super.getStackTrace(); } @Override public synchronized Throwable initCause(Throwable arg0) { // TODO Auto-generated method stub return super.initCause(arg0); } @Override public void printStackTrace() { // TODO Auto-generated method stub super.printStackTrace(); } @Override public void printStackTrace(PrintStream arg0) { // TODO Auto-generated method stub super.printStackTrace(arg0); } @Override public void printStackTrace(PrintWriter arg0) { // TODO Auto-generated method stub super.printStackTrace(arg0); } @Override public void setStackTrace(StackTraceElement[] arg0) { // TODO Auto-generated method stub super.setStackTrace(arg0); } @Override public String toString() { // TODO Auto-generated method stub return super.toString(); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub return super.equals(obj); } @Override protected void finalize() throws Throwable { // TODO Auto-generated method stub super.finalize(); } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } }
JdbcUtils_Tomcat位于utils包
package cn.itcast.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; /*开源连接池的第3种:使用Tomcat服务器创建web应用的连接池 * 1,服务器找servlet,servlet调用service,service调用dao,dao调用JdbcUtils * 2,由于需要Tomcat启动时就为web应用创建连接池 * 所以需要将mysql驱动拷贝到Tomcat\lib目录 * 否则:javax.naming.NamingException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver' * 3,在WebRoot\META-INF\新建context.xml,在该文档中, * 配置Tomcat即将创建的连接池的各个属性 * 4,JdbcUtils获得服务器为应用创建的连接池,使用固定三句代码, * 分别是初始化JNDI, * 找到JNDI, * 在JNDI中找到Tomcat服务器启动时,根据context.xml为web应用专门配置的连接池Datasource, * 并将该连接池赋值给JdbcUtils的成员ds * 完成方法:getConnection和release * 5,删除webapp的时候,记得将服务器\conf\Catalina\localhost\web应用名.xml一并删除! */ public class JdbcUtils_Tomcat { //定义成员记住DBCP创建出来的数据源(即连接池) private static DataSource ds; static{ /*以下代码,拷贝于Tomcat文档\8JNDI\4JDBC DataSource * 标准三步曲: * 1,初始化JNDI容器 * 2,根据名称java:comp/env找到JNDI容器 * 3,根据context.xml中绑定的名字jdbc/DataSourceInJND,找到DataSource */ Context initCtx; try { initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); ds = (DataSource)envCtx.lookup("jdbc/DataSourceInJND"); } catch (NamingException e) { throw new ExceptionInInitializerError(e); } } //方法1:获取已装饰的连接 public static Connection getConnection() throws SQLException{ //Tomcat用的其实也是DBCP生成的连接,该连接是已经过装饰后的Connection, //覆写了close方法,即归还到数据源(即连接池) return ds.getConnection(); } //方法2:释放连接 public static void release(Connection conn,Statement st,ResultSet rs){ if (conn!=null) { try { conn.close(); }catch (Exception e) { //只能记录!一旦抛出,后面的2条if代码就无法执行了 e.printStackTrace(); } conn=null; } if (st!=null) { try { st.close(); }catch (Exception e) { //只能记录!一旦抛出,后面的1条if代码就无法执行了 e.printStackTrace(); } st=null; } if (rs!=null) { try { rs.close(); }catch (Exception e) { e.printStackTrace(); } rs=null; } } }
TomcatPool_Servlet位于web.controlle包
package cn.itcast.web.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.dao.Dao; public class TomcatPool_Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*开源连接池的第3种:使用Tomcat服务器创建web应用的连接池 * 1,服务器找servlet,servlet调用service,service调用dao,dao调用JdbcUtils * 2,由于需要Tomcat启动时就为web应用创建连接池 * 所以需要将mysql驱动拷贝到Tomcat\lib目录 * 3,在WebRoot\META-INF\新建context.xml,在该文档中, * 配置Tomcat即将创建的连接池的各个属性 * 4,JdbcUtils获得服务器为应用创建的连接池,使用固定三句代码, * 分别是初始化JNDI, * 找到JNDI, * 在JNDI中找到Tomcat服务器启动时,根据context.xml为web应用专门配置的连接池Datasource, * 并将该连接池赋值给JdbcUtils的成员ds * 完成方法:getConnection和release * 5,删除webapp的时候,记得将服务器\conf\Catalina\localhost\web应用名.xml一并删除! */ Dao dao=new Dao(); dao.add(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
context.xml位于WebRoot\META-INF\目录
部署后,会自动以Web应用名.xml生成在Tomcat\conf\Catalina\localhost\目录下!
如:day16_TomcatPool.xml,所以删除web应用时,要记得将
该目录下的Web应用名.xml一并删除!
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/DataSourceInJND" auth="Container" type="javax.sql.DataSource" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/day16" initialSize="10" maxActive="30" maxIdle="4"/> </Context>