tomcat的JNDI配置及测试

tomcat的JNDI配置及测试
本示例相关:
项目名称:proj
数据库为:Oracle10g
数据库名:db
数据库用户名:user
数据库用户密码:user
测试所用的表名:t_user
1.tomcat的配置文件server.xml
  在server.xml文件的GlobalNamingResources属性中加入以下语句:
  <Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver"    maxActive="20" maxIdle="10" maxWait="-1" name="WFDataSource" password="user"   

type="javax.sql.DataSource" url="jdbc:oracle:thin:@127.0.0.1:1521:db" username="user"/>

2.tomcat的配置文件context.xml
   在context中加入以下语句:
   <ResourceLink name="WFDataSource" type="javax.sql.DataSource" global="WFDataSource"/>

3.项目web-inf目录下的web.xml
  在文件的webapp属性下加入以下语句:
  <resource-ref>   
     <description>DBConnection</description>   
     <res-ref-name>proj</res-ref-name>
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth>
   </resource-ref>
  
4.配置完成后的测试:
  新建jsp页面,黏贴如下代码:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>JDBC Test</title>
</head>
<body>
<%

  Connection conn=null;
  Statement stmt=null;
  ResultSet rs=null;
  ResultSetMetaData md=null;
  try{ Context initCtx=new InitialContext();   
       DataSource ds=(DataSource)initCtx.lookup("java:comp/env/proj");
       if(ds!=null){                   
                    out.println("已经获得DataSource");     
                    out.println(ds.toString());   
                    conn=ds.getConnection();
                    stmt=conn.createStatement();  
                    out.println("aa");
                    rs=stmt.executeQuery("select * from t_user");  
                    md=rs.getMetaData();                                                      
                    out.println("<table border=1>");  
                    out.println("<tr>");
                    for(int i=0;i<md.getColumnCount();i++)                                                 {             
                         out.println("<td>"+md.getColumnName(i+1)+"</td>");    
                              }   
                 while(rs.next())    
                 {                     
                           out.println("<tr>"); 
                           out.println("<td>"+rs.getString(1)+"</td>");
                           out.println("<td>"+rs.getString(2)+"</td>");
                           out.println("<td>"+rs.getString(3)+"</td>");
                           out.println("<td>"+rs.getString(4)+"</td>");
                           out.println("</tr>");                       
                 }
                          out.println("</table>");
                          conn.close();             
                              }                
                                     }    
    catch(Exception e)  {  
                        out.println(e.toString()); 
                        System.out.println(e.toString());
  }%>
   </body>
</html>
5.启动tomcat,运行步骤4的jsp页面,如果出现数据则表示连接池配置成功

你可能感兴趣的:(sql,tomcat,jsp,xml,SQL Server)