wood burning stoves 2.0*
The moose likes JDBC and the fly likes DataSource implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DataSource implementation" Watch "DataSource implementation" New topic
Author

DataSource implementation

shishir gupta
Greenhorn

Joined: Apr 30, 2004
Posts: 28
Using Tomcat3.1 and JBoss3.2.3. Want to deploy this application on JBoss.

Registered the datasource with a JNDI naming service.

----------------------------------------------------------

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Connect{

String stat="Not connected";
public void init() {
try{
Context ctx=new InitialContext();
if(ctx==null) throw new Exception("no context");

DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
if(ds!=null) {
Connection con=ds.getConnection();
if(con!=null){
stat="Got connection " +con.toString();
con.close();
}
}
} catch(Exception e) {e.printStackTrace();}
}

public String getStat() {
return stat;
}
}

------------------------------------------------------

I have put this class file in WEB-INF/classes folder.

In web.xml i made the following entries:

<description>JDBC Test Application </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>


This is the jsp page from which i am trying to test my connection.
------------------------------------------------------

<html>
<head><title> Connection Test </title></head>
<body>
<%
Connect conn=new Connect();
conn.init();
%>
<h3>Connection Result </h3>
<%=conn.getStat() %>
</body>
</html>

when i run this jsp page i get an error "Not connected"
----------------------------------------------------------

javax.naming.NameNotFoundException: No object bound for java:comp/env/jdbc/BooksDB
at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:116)
at javax.naming.InitialContext.lookup(Unknown Source)
at example.jdbcExample.Connect.init(Connect.java:15)
at jsp._0002fjsp_0002fconnect_0002ejspconnect_jsp_0._jspService(_0002fjs
p_0002fconnect_0002ejspconnect_jsp_0.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:296)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:369)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:368)
at org.apache.tomcat.core.Handler.service(Handler.java:261)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:356)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:720)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:666)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnectio
n(HttpConnectionHandler.java:194)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:403)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Unknown Source)
--------------------------------------------
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
javax.naming.NameNotFoundException: No object bound for java:comp/env/jdbc/BooksDB


I have no idea about JBoss but from the exception description it is clear that you haven't bound the Datasource object with java:comp/env/jdbc/BooksDB JNDI Name. See your container documentation to do this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataSource implementation