| Author |
Not able to connect to a database
|
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) -------------------------------------------- [ May 13, 2004: Message edited by: Bear Bibeault ]
|
 |
Mike Finn
Greenhorn
Joined: May 19, 2004
Posts: 6
|
|
|
Using JMX console/JNDIView, can you see the env entry in the JNDI tree?
|
 |
Wayne L Johnson
Ranch Hand
Joined: Sep 03, 2003
Posts: 399
|
|
You have shown us all of the pieces except for one: where the "jdbc/BooksDB" resource is defined. With TOMCAT you need to have a "<Context...> ... </Context>" tag for your web application. Within that tag you must define the "<Resource ...>" and "<ResourceParams ...>" tags for the datasource. [Alternately they could be in the "<GlobalNamingResources>" tag, but that's usually a bad idea for most situations.] If it's not defined correctly, then the lookup will fail. Could you show us how that's defined? [ May 20, 2004: Message edited by: Wayne L Johnson ]
|
 |
 |
|
|
subject: Not able to connect to a database
|
|
|