最简单的JNDI对数据库操作 Tomcat中配置数据源

时间:2021-01-10 19:13:28

最简单的JNDI对数据库操作

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="javax.sql.DataSource"/>
<jsp:directive.page import="java.sql.Connection"/>
<jsp:directive.page import="java.sql.ResultSet"/>
<jsp:directive.page import="javax.naming.InitialContext"/>
<jsp:directive.page import="java.sql.SQLException"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  <%
       DataSource ds = null;
     Connection conn = null;
     ResultSet rs = null;
     try{
      InitialContext ctx=new InitialContext();
      ds=(DataSource)ctx.lookup("java:comp/env/jdbc/e3java");
      conn= ds.getConnection();
      java.sql.Statement stmt = conn.createStatement();
        //提示:users必须是数据库已有的表,
        //这里的数据库前文提及的Data Source URL配置里包含的数据库。
      String strSql = "select * from product";
       rs= stmt.executeQuery(strSql);
      while(rs.next()){
         System.out.println(rs.getString(1));              
        }
      }catch(Exception ex){
       ex.printStackTrace();
      }finally{
       try {
     rs.close();
        conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
     }
  %>
  <body>
    This is my JSP page. <br>
  </body>
</html>

 

Tomcat中配置数据源

 

<!--

    Context configuration file for the Tomcat Administration Web App

    $Id: e3.xml,v 1.1 2006/10/02 02:32:16 zyz Exp $

-->


<Context path="/" docBase="项目目录路径"
        debug="0" privileged="true" reloadable="true" >

  <!-- Uncomment this Valve to limit access to the Admin app to localhost
   for obvious security reasons. Allow may be a comma-separated list of
   hosts (or even regular expressions).
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
    allow="127.0.0.1"/>
  -->

  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_e3_log." suffix=".txt"
          timestamp="true"/>

 <Resource name="jdbc/e3java" auth="Container" type="javax.sql.DataSource"/> 

  <ResourceParams name="jdbc/e3java">
   <parameter>     
    <name>factory</name>    
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>   
   </parameter>
   <parameter>     
    <name>driverClassName</name>    
    <value>oracle.jdbc.driver.OracleDriver</value>   
   </parameter>
    <parameter>
                <name>maxActive</name>
                <value>100</value>
             </parameter>
             <parameter>
                  <name>maxIdle</name>
                   <value>30</value>
               </parameter>
             <parameter>
                 <name>maxWait</name>
                  <value>10000</value>
            </parameter>
   <parameter>     
    <name>url</name>
                <value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
   </parameter>   
   <parameter>     
    <name>username</name>     
    <value>zqtxw</value>   
   </parameter>   
   <parameter>     
    <name>password</name>     
    <value>111111</value>   
   </parameter>
   <parameter>
    <name>removeAbandoned</name>
    <!-- Abandoned DB connections are removed and recycled -->
    <value>true</value>
   </parameter>
   <parameter>
     <name>removeAbandonedTimeout</name>
     <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned.  -->
     <value>180</value>
   </parameter>
    <parameter>
     <name>logAbandoned</name>
    <!-- Log a stack trace of the code which abandoned -->
      <value>true</value>
    </parameter>
  </ResourceParams>
</Context>