拿mysql数据库和tomcat5.5为例子:
安装tomcat5.5(注意这点)
安装mysql
拷贝mysql驱动到tomcat_home/common/lib下
新建一个web工程jndi
在工程中加入index.jsp
在web.xml中加入
在tomcat_home/conf/localhost/下建立一个xml文件,文件名是 <yourAppName> .xml
例如我的工程名叫jndi,对应的名字叫jdni.xml
内容如下:
运行测试:
打开ie,输入http://localhost:8080/jndi/index.jsp
看到success 代表配置成功
安装tomcat5.5(注意这点)
安装mysql
拷贝mysql驱动到tomcat_home/common/lib下
新建一个web工程jndi
在工程中加入index.jsp
-
HTML code
-
<% @page import = " java.util.*,javax.naming.*,java.sql.*,javax.sql.* " %>
<% @page contentType = " text/html;charset=BIG5 " %>
<%
Context ctx = new InitialContext();
String strLookup = " java:comp/env/jdbc/test " ;
DataSource ds = (DataSource) ctx.lookup(strLookup);
Connection con = ds.getConnection();
if (con ! = null ){
out.print( " success " );
} else {
out.print( " failure " );
}
%>
在web.xml中加入
-
XML code
-
< resource-ref >
< res-ref-name > jdbc/test </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
< res-sharing-scope > Shareable </ res-sharing-scope >
</ resource-ref >
在tomcat_home/conf/localhost/下建立一个xml文件,文件名是 <yourAppName> .xml
例如我的工程名叫jndi,对应的名字叫jdni.xml
内容如下:
-
XML code
-
< Context >
< Resource
name ="jdbc/test"
type ="javax.sql.DataSource"
password ="bb"
driverClassName ="com.mysql.jdbc.Driver"
maxIdle ="2"
maxWait ="50"
username ="root"
url ="jdbc:mysql://localhost:3306/test"
maxActive ="4" />
</ Context >
运行测试:
打开ie,输入http://localhost:8080/jndi/index.jsp
看到success 代表配置成功