于一 发表于 2015-8-12 09:35:18

在tomcat中配置连接池

  部署到tomcat后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下
  1、 把oracle驱动jar包拷贝到<CATALINA_HOME>/lib目录;
      在MyEclipse中新建web project工程,并且把 oracle驱动jar 加入到工程的buildpath中。
--------------------------------------------------------------------------------------------------------------------
2、 在工程的webroot/META-INF目录下增加context.xml文件(实际反映到<CATALINA_HOME>/webapps/app应用/ 目录下),其内容:
<?xml version="1.0" encoding="UTF-8"?>
<Contextreloadable="true" >
<Resource name="jdbc/1872" auth="Container" type="javax.sql.DataSource"
   maxActive="100" maxIdle="30" maxWait="10000"
   username="equipmanage" password="test"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   url="jdbc:oracle:thin:@192.168.187.9:1521:hngolf"/>
</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>1872 Connection</description>
                <res-ref-name>jdbc/1872</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/1872 和 第二步中的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/1872");
                        return ds.getConnection();
                } catch (NamingException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                return null;
      }
}
      说明: 注意 高亮部分中的 jdbc/1872 和 第二步中的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>
页: [1]
查看完整版本: 在tomcat中配置连接池