This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Tomcat and the fly likes JNDI datasource question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "JNDI datasource question" Watch "JNDI datasource question" New topic
Author

JNDI datasource question

Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
I am doing a very simple test application trying to connect MSSQL database from my JSP page

I defined a datasource using Tomcat web Server. I see following entry in server.xml file:




In my JSP I am getting an error on this line:



Do you see any obvious error?


The error:

javax.servlet.ServletException: Name LTdata is not bound in this Context


amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi,
Declare the Resource reference in web.xml file as below


Regards,
Amit
Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
Thanks Amit for your reply

I did your recommended change, now I am getting:

javax.naming.NameNotFoundException: Name LTdata is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)

Any suggestion?

Thanks again for your help, (btw, I am also punekar )
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
hi,
there is an extra "/" in "java:/comp/env/LTdata".
It should be "java:comp/env/LTdata".
Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
Thanks amit I removed that extra slash earlier.
Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
I am getting a little different error now:

java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)

I am connecting to MSSQL so I have 2 dll

\webapps\gdesktop\WEB-INF\lib\sqljdbc4.jar
\webapps\gdesktop\WEB-INF\lib\sqljdbc.jar
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

Since JNDI is a server resource, you have to put your JDBC driver where the server can find it. I have mine in $CATALINA_HOME/common/lib


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
I got it in common/lib directory

I opened the page where I get the error and then tried to move the 2 files sqljdbc4.jar & sqljdbc.jar. I got the error that file is in use and cnnot be moed. So looks like the jar files are getting used but still I get this error:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.genesyslab.uadthin.filter.CachePreventFilter.doFilter(CachePreventFilter.java:30)
com.genesyslab.uadthin.filter.UTF8Filter.doFilter(UTF8Filter.java:127)


root cause

javax.servlet.ServletException: Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
org.apache.jsp.custom.ltCustomerData_jsp._jspService(ltCustomerData_jsp.java:85)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.genesyslab.uadthin.filter.CachePreventFilter.doFilter(CachePreventFilter.java:30)
com.genesyslab.uadthin.filter.UTF8Filter.doFilter(UTF8Filter.java:127)


root cause

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
org.apache.jsp.custom.ltCustomerData_jsp._jspService(ltCustomerData_jsp.java:61)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.genesyslab.uadthin.filter.CachePreventFilter.doFilter(CachePreventFilter.java:30)
com.genesyslab.uadthin.filter.UTF8Filter.doFilter(UTF8Filter.java:127)


root cause

java.sql.SQLException: No suitable driver
java.sql.DriverManager.getDriver(DriverManager.java:243)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
org.apache.jsp.custom.ltCustomerData_jsp._jspService(ltCustomerData_jsp.java:61)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.genesyslab.uadthin.filter.CachePreventFilter.doFilter(CachePreventFilter.java:30)
com.genesyslab.uadthin.filter.UTF8Filter.doFilter(UTF8Filter.java:127)

Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
My JSP code:




server.xml looks like this:

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
Hi,
You need to put the JAR files of SQL driver in <<tomcat_dir>>\lib so that they are visible to Server runtime.

Nikhi l Shah
Greenhorn

Joined: Sep 10, 2009
Posts: 12
Friends Thank you very much for your help!!

Adding this entry to the context.xml file fixed my issue:

<Context>
<Resource name="LTdb" auth="Container"
type="javax.sql.DataSource" username="username" password="password"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://servername:1433;DatabaseName=ocs_irv;"
maxActive="8" maxIdle="4"/>
Ram Para
Ranch Hand

Joined: Jul 09, 2008
Posts: 50
Try after adding the following three Jars to lib.

msbase.jar
mssqlserver.jar
msutil.jar

Ram Parashar
http://doinfinite.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JNDI datasource question
 
Similar Threads
Can't persist data into Oracle 10g by the default persistence layer of EJB 3.0 (Hibernate)
Error creating bean with name 'datasource': Invocation of init method failed; comp not bound
Warnings with message resource
Using properties file for defining class dependency
use of type in <jsp:usebean>