Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
    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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

BasicDataSource cannot be cast to javax.naming

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Marshal
Posts: 22394
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Once is enough, thank you. UseOneThreadPerQuestion. And I already gave you an answer in that thread that you apparently haven't even read.
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic