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 .

阅读更多
上一篇JBoss4.0.3 + Oracle 9i (JNDI)
下一篇JBoss4.0.5+Mysql5.0.41 JNDI
想对作者说点什么? 我来说一句

在Tomcat中配置数据源

2008年04月26日 47KB 下载

tomcat5.5JNDI连接数据库

2008年11月27日 1.05MB 下载

没有更多推荐了,返回首页

关闭
关闭