• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------
I 've been trying to connect between tomcat 5.5 and jsp and whenver i execute i get this error--
--------

org.apache.jasper.JasperException: Exception in JSP: /grace.jsp:22

19: try{
20: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
21: Context initCtx = new InitialContext();
22: Context envCtx = (Context) initCtx.lookup("java:comp/env/jdbc/start");
23: DataSource ds = (DataSource) envCtx.lookup("jdbc/start");
24: conn = ds.getConnection();
25: stmt = conn.createStatement();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:489)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context
org.apache.jsp.grace_jsp._jspService(grace_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

-----------------
this is my grace.jsp
-----------------
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.naming.*"%>
<%@page import="javax.sql.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<BODY>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env/jdbc/start");
DataSource ds = (DataSource) envCtx.lookup("jdbc/start");
conn = ds.getConnection();
stmt = conn.createStatement();
String db = "jdbc:odbc:Driver = {Microsft Access Driver}; DBQ= saveme.accdb";
rs = stmt.executeQuery("SELECT * FROM account");
%><BR>Password<BR><%
while( rs.next())
{
%>this is a line from the DB: "<%= rs.getString("username")%>" <BR><%
}
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.close(); // Return to connection pool
conn = null; // Make sure we do not close it twice
} catch (SQLException e) {
out.print("Throw e" + e);
} finally {
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}

%>
</BODY>
</HTML>

-------------------
web.xml
-----------
<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/start</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>

-------------
context.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<context reloadable="true" path="/chance" debug="5" crosscontext="true" docbase="%catalina_home%/webapps/chance">
<logger timestamp="true" suffix=".txt"
prefix="localhost_realtorApp_log."
classname="org.apache.catalina.logger.FileLogger">
</logger>
<resource name="jdbc/start"
username=""
password=""
url="jdbc:odbc:start"
type="javax.sql.DataSource"
auth="Container"
driverclassname="sun.jdbc.odbc.JdbcOdbcDriver"
maxwait="-1"
maxactive="4"
maxidle="10"
factory="org.apache.commons.dbcp.BasicDataSourceFactory">
</resource>
</context>
<welcome-file>grace.jsp</welcome-file>
</welcome-file-list>
</web-app>
------------------------
i hope someone can settle this issue.... please help....
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aswin Gopan wrote:Context envCtx = (Context) initCtx.lookup("java:comp/env/jdbc/start");

...

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.naming.Context


Seeing as the ClassCastException occurred because you tried to cast something to Context. The only cast to Context I can see is the above line. So it appears that initCtx.lookup("java:comp/env/jdbc/start") returns a BasicDataSource, and you should cast the result to DataSource, not Context.

That makes sense since in the resource "jdbc/start" you've set the type to javax.sql.DataSource.
 
Seriously? That's what you're going with? I prefer this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic