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:
< 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 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:
< 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 .