• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JNDI DataSource unable to work !!

kmthien

Senior member
Hi guys,


/usr/local/tomcat5/webapps/Advanced/WEB-INF/web.xml
===========================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>mySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/BooksDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
===========================================================

/usr/local/tomcat5/conf/server.xml
===========================================================
.............

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
suffix=".txt" timestamp="true"/>
<ResourceParams name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.3.57:3306/books</value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
===========================================================

/usr/local/tomcat5/webapps/Advanced/connect.jsp
===========================================================
<html>
<head>
<title>Connection Test</title>
</head>
<body>

<%
com.wrox.library.Connect con = new com.wrox.library.Connect();
con.init();
%>

<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
===========================================================

/usr/local/tomcat5/webapps/Advanced/WEB-INF/classes/com/wrox/library/Connect.java
===========================================================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Connect {

String stat = "Not Connected";

public void init() {
try {
stat = "1";
Context ctx = new InitialContext();
stat = "2";
if(ctx == null )
throw new Exception("Oops - No Context");
stat = "3";
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";
if (ds != null) {
Connection conn = ds.getConnection();
stat = "5";
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}

public String getstat() {
return stat;
}
}
===========================================================

When I tried to display connect.jsp, what I got display on IE6 is as follow:
=================================================
Connection Results
4
==================================================

Which means that the Connect.class unable to get the DataSource, for which the DataSource is NULL. Pls help !!
 
Back
Top