This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Not able to connect to a database  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JMX console/JNDIView, can you see the env entry in the JNDI tree?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!