[示例代码]JDBC数据源配置(JDBC Data Sources)

项目目录结构如上所示。

数据库是Oracle9i。

 

1. 把class12.jar包放到Tomcat的lib目录下。如果是class12.zip的话,把扩展名改一下就行了。

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
 <description>this is a test for jndi on Oracle</description>
 <res-ref-name>jdbc/dbname</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>container</res-auth>
</resource-ref>
</web-app>

3. tom.xml

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="/tom" path="/tom" reloadable="true" workDir="/tom/work">
   <Resource name="jdbc/dbname" type="javax.sql.DataSource" auth="container"/>
  <ResourceParams name="jdbc/dbname">
  <parameter>
  <name>username</name>
  <value>scott</value>
  </parameter>
  <parameter>
  <name>password</name>
  <value>tiger</value>
  </parameter>
  <parameter>
  <name>driverClassName</name>
  <value>oracle.jdbc.driver.OracleDriver</value>
  </parameter>
  <parameter>
  <name>url</name>
  <value>jdbc:oracle:thin:@127.0.0.1:1521:dbname</value>
  </parameter>
  <parameter>
    <name>maxActive</name>
    <value>20</value>
  </parameter>
  <parameter>
    <name>maxIdle</name>
    <value>10</value>
  </parameter>
  <parameter>
    <name>maxWait</name>
    <value>-1</value>
  </parameter>

  </ResourceParams>

</Context>

4.测试用JSP:  dstest.jsp

<%@ page language="java" pageEncoding="GB2312" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.sql.*" %>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Test Tom Config</title>
</head>
<body bgcolor="#FFFFFF">

<%
 try {
            Context initctx=new InitialContext();
            Context envctx=(Context) initctx.lookup("java:comp/env");
            DataSource ds=(DataSource)envctx.lookup("jdbc/dbname");
            Connection conn=ds.getConnection();
            Statement stmt=conn.createStatement();
            String sql="select * from emp where empno = '7934'";
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
%>
<table width="787" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="787" height="62" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
        <!--DWLayoutTable-->
        <tr>
          <td width="787" height="62"><h1>Generic JavaBean Resources Test</h1></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td height="159"><table width="75%" border="0">
        <tr>
          <td width="44%"><div align="right">Empno:</div> </td>
          <td width="56%"><%= rs.getString("empno")%></td>
        </tr>
        <tr>
          <td><div align="right">ename:</div></td>
          <td><%= rs.getString("ename")%></td>
        </tr>
      </table></td>
  </tr>
</table>
<%
       rs.close();
       stmt.close();
       conn.close();
       }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
%>

</body>
</html>

 

 

你可能感兴趣的:([示例代码]JDBC数据源配置(JDBC Data Sources))