Tomcat5.5 + Mysql5.0 JNDI

1. Modify the server.xml (path: $TOMCAT_HOME$/conf)Add :
<resource   
auth="Container"   
name="mysql/test"   
type="javax.sql.DataSource"   


driverClassName="com.mysql.jdbc.Driver"   
password=""   
maxIdle="30"   
maxWait="10000"   
username="root"   
url="jdbc:mysql://localhost:3306/strutsarticle"   
maxActive="20">
between <globalnamingresources />  </globalnamingresources />.

2.Modify Context.xml (path:$TOMCAT_HOME$/conf)
<resourcelink type="javax.sql.DataSourcer" global="mysql/test" name="mysql/test" />
comment: defined a global parameter for the JNDI name.

3.Create MyWebProject.xml , Content like flow: 

<? xml version="1.0" encoding="UTF-8" ?>
< Context
    
crossContext ="true"
    reloadable
="true" >
  
< Resource
    
auth ="Container"
    name
="mysql/test"
    type
="javax.sql.DataSource"
    driverClassName
="com.mysql.jdbc.Driver"
    password
=""
    maxIdle
="30"
    maxWait
="10000"
    username
="root"
    url
="jdbc:mysql://localhost:3306/strutsarticle"
    maxActive
="20" />
  
< ResourceLink
    
global ="mysql/test"
    name
="mysql/test"
    type
="javax.sql.DataSourcer" />
</ Context >

after create the MyWebProject , save it into (path:$TOMCAT_HOME$/conf/Catalina/localhost)


4.create a jsp page(named index.jsp) for testing (create a folder into $TOMCAT_HOME$/webapps  named MyWebProject ) , jsp page's content like flow:

 

<% @ page contentType = " text/html;charset=gb2312 "   %>
<% @ page  import = " java.sql.* " %>
<% @ page  import = " javax.sql.* " %>
<% @ page  import = " javax.naming.* " %>
<% @ page session = " false "   %>
< html >
< head >
< title ></ title >
<%  
   out.print(
" 我的测试开始 " + " <br/> " );
   DataSource ds 
=   null ;
   
try {
   InitialContext ctx
=new InitialContext();
   ds
=(DataSource)ctx.lookup("java:comp/env/mysql/test");
   Connection conn 
= ds.getConnection();
   Statement stmt 
= conn.createStatement();
   String strSql 
= " select * from test";
   ResultSet rs 
= stmt.executeQuery(strSql);
   
while(rs.next()){
      out.print(rs.getString(
2)+"<br/>");                 
   }

   rs.close();
   stmt.close();
   conn.close();
 out.print(
"我的测试结束");
   }

   
catch (Exception ex) {
       out.print(
"出现例外,信息是:"+ex.getMessage());
    ex.printStackTrace();
   }

%>
</ head >
< body >
</ body >
</ html >

5. create a config file (web.xml) for u application (path :$TOMCAT_HOME$/webapps/MyWebProject/WEB-INF), like this:

 

<? xml version="1.0" encoding="ISO-8859-1" ?>

< web-app  xmlns ="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version
="2.4" >

  
< display-name > Welcome to Tomcat </ display-name >
  
< description >
     Welcome to Tomcat
  
</ description >

<!--  JSPC servlet mappings start  -->

    
< servlet >
        
< servlet-name > org.apache.jsp.index_jsp </ servlet-name >
        
< servlet-class > org.apache.jsp.index_jsp </ servlet-class >
    
</ servlet >
    
<!--  JSPC servlet mappings end  -->
< resource-ref >
    
< description > mysqlDB Connection </ description >
    
< res-ref-name > mysql/test </ res-ref-name >
    
< res-type > javax.sql.DataSource </ res-type >
    
< res-auth > Container </ res-auth >
</ resource-ref >
</ web-app >

6. run the Tomcat server , and enter the http://127.0.0.1:8080/MyWebProject/,is">URL:http://127.0.0.1:8080/MyWebProject/  , it's ok .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值