首先在eclipse下新建一个名为jndi的动态web项目;
打开其目录下的web.xml,进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>
jndi</display-name>
<resource-ref>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后对server.xml进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="80" redirectPort="8443">
</Connector>
<Connector connectionTimeout="-1" port="8009" protocol="AJP/1.3" redirectPort="8443">
</Connector>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host appBase="webapps" name="localhost">
<Context docBase="jndi" path="/jndi" reloadable="true" source="org.eclipse.jst.j2ee.server:jndi">
<Resource
name="jdbc/oracle"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
password="tiger"
maxIdle="2"
maxWait="5000"
username="scott"
url="jdbc:oracle:thin:@localhost:1521:guitar"
maxActive="4"/>
</Context>
</Host>
</Engine>
</Service>
</Server>
最后写一个名为test.jsp的测试页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%!final String JNDINAME = "java:comp/env/jdbc/oracle";%>
<%
Connection conn = null;
try {
// 初始化查找命名空间
Context ctx = new InitialContext();
// 找到DataSource
DataSource ds = (DataSource) ctx.lookup(JNDINAME);
conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select * from emp");
while (rs.next()) {
int size = rs.getMetaData().getColumnCount();
for (int i = 1; i <= size; i++) {
out.println(rs.getString(i) + "<br/>");
}
}
} catch (Exception e) {
System.out.println(e);
}
%>
<%=conn%>
<%
// 将连接重新放回到池中
conn.close();
%>
至此,配置对oracle的jndi数据源已经列出相关最重要的配置代码,需要注意的是,必须在项目中导入相对应的数据库驱动jar包,本例子中导入
ojdbc14.jar.需要注意的是代码中加粗部分,可能需要根据你的实际情况有所改变,
JNDINAME的名字是一一对应的.server.xml的配置有许多方法,这里就不一一细述,网上例子很多.
本人水平有限,欢迎高手和我交流批评^_^