在tomcat中设置数据库连接池
在tomcat中设置数据库连接池 -------------------------------------------------------------------------------------------------------------------- 1、 把mysql驱动jar包mysql-connector-java-5.1.6.jar拷贝到<CATALINA_HOME>/lib目录; 在MyEclipse中新建web project工程,并且把 mysql-connector-java-5.1.6.jar 加入到工程的buildpath中。 -------------------------------------------------------------------------------------------------------------------- 2、 在工程的webroot/META-INF目录下增加context.xml文件(实际反映到<CATALINA_HOME>/webapps/app应用/ 目录下),其内容: <?xml version="1.0" encoding="UTF-8"?> <Context reloadable="true" > <Resource name="jdbc/stuDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="1234" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=gbk"/> </Context> -------------------------------------------------------------------------------------------------------------------- 3、 在web.xml中引用数据库连接池资源: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>"> <resource-ref> <description>student DB Connection</description> <res-ref-name>jdbc/stuDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 说明: 注意 jdbc/stuDB 和 第二步中的context.xml中 resource的name属性的值保持一致。 -------------------------------------------------------------------------------------------------------------------- 4、 编写cn.tsp2c.stu.JdbcUtil.java package cn.tsp2c.stu; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public final class JdbcUtil { public static Connection getConnection() { DataSource ds = null; Context context = null; try { context = new InitialContext(); ds = (DataSource) context.lookup("java:comp/env/jdbc/stuDB"); return ds.getConnection(); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } } 说明: 注意 高亮部分中的 jdbc/stuDB 和 第二步中的context.xml中 resource的name属性的值保持一致。 -------------------------------------------------------------------------------------------------------------------- 5、编写index.jsp <%@ page language="java" import="java.util.*,cn.tsp2c.stu.*" pageEncoding="gbk"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.SQLException"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form method="post" name="stuform" action=""> <%! Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; %> <% try{ conn = JdbcUtil.getConnection(); if(conn == null){ // } String sqlStr = "select * from tb_student"; psmt = conn.prepareStatement(sqlStr); rs = psmt.executeQuery(); %> <table border="1"> <tr><th>学号</th><th>姓名</th><th>年龄</th><th>性别</th><th>电话</th><th>住址</th></tr> <% while(rs.next()){ %> <tr> <td><%= rs.getString("sid")%></td> <td><%= rs.getString("sname")%></td> <td><%= rs.getInt("age")%></td> <td><%= rs.getString("gender")%></td> <td><%= rs.getString("phone")%></td> <td><%= rs.getString("address")%></td> </tr> <% } }catch(Exception e){ e.printStackTrace(); }finally{ try{ rs.close(); psmt.close(); conn.close(); }catch(SQLException e){ e.printStackTrace(); } } %> </table> </form> </body> </html> |