我就是想测试下jndi 遇到问题
jsp文件
- HTML code
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <html> <body> <% /* Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; String user="scott"; String password="tiger"; Connection conn= DriverManager.getConnection(url,user,password); */ String JNDI_STR = "java:comp/env/FirstJNDI"; Context ctx= new InitialContext(); DataSource ds = (Datasource) ctx.lookup(JNDI_STR); connection conn = ds.getConnection(); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from emp"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) {%> <%=rs.getString(1)%> <%=rs.getString(2)%> <%out.print("<br>");%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); conn.close(); %> </body> </html>
server.xml中增加
- HTML code
<Context path="/TestJNDI" docBase="Tomcat 5.5\webapps\TestJNDI" debug="5" reloadable="true" crossContext="true" workDir=""> <Resource name="FirstJNDI" auth="Container" type ="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username = "scott" password="tiger" dirverClassName ="oracle.jdbc.OracleDriver" url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/></Context>
但是 启动Tomcat 时报错
- Assembly code
2011-5-29 22:49:08 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.20.
2011-5-29 22:49:08 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], ra
ndom [true].
2011-5-29 22:49:08 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8081
2011-5-29 22:49:08 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2011-5-29 22:49:08 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 922 ms
2011-5-29 22:49:08 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2011-5-29 22:49:08 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.33
2011-5-29 22:49:08 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
2011-5-29 22:49:08 org.apache.catalina.core.StandardContext resourcesStart
严重: Error starting static Resources
java.lang.IllegalArgumentException: Document base C:\Program Files\Apache Softwa
re Foundation\Tomcat 5.5\webapps\Tomcat 5.5\webapps\TestJNDI does not exist or i
s not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.
java:141)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardConte
xt.java:3895)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
064)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)