aspose file tools*
The moose likes JDBC and the fly likes BasicDataSource cannot be cast to javax.naming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

BasicDataSource cannot be cast to javax.naming

Aswin Gopan
Greenhorn

Joined: Apr 12, 2011
Posts: 3
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 = "jdbcdbcriver = {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="jdbcdbc: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....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Once is enough, thank you. UseOneThreadPerQuestion. And I already gave you an answer in that thread that you apparently haven't even read.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BasicDataSource cannot be cast to javax.naming