Reputation: 163
Good Morning,
Here is my JSP code (note, it is just a slightly modified source from the Oracle Sample Application page here: http://docs.oracle.com/cd/A97336_01/buslog.102/a83726/sampapp2.htm)
<%@ page import="java.sql.*" %>
<HTML>
<HEAD> <TITLE> The JDBCQuery JSP </TITLE> </HEAD>
<BODY BGCOLOR=white>
<% String searchCondition = request.getParameter("cond");
if (searchCondition != null) { %>
<H3> Search results for : <I> <%= searchCondition %> </I> </H3>
<%= runQuery(searchCondition) %>
<HR><BR>
<% } %>
<B>Enter a search condition:</B>
<FORM METHOD=get>
<INPUT TYPE="text" NAME="cond" SIZE=30>
<INPUT TYPE="submit" VALUE="Ask Oracle">
</FORM>
</BODY>
</HTML>
<%!
private String runQuery(String cond) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection((String)session.getAttribute ("url"),
"username", "pw");
stmt = conn.createStatement();
rset = stmt.executeQuery ("SELECT ename, sal FROM scott.emp "+
(cond.equals("") ? "" : "WHERE " + cond ));
return (formatResult(rset));
} catch (SQLException e) {
return ("<P> SQL error: <PRE> " + e + " </PRE> </P>\n");
} finally {
if (rset!= null) rset.close();
if (stmt!= null) stmt.close();
if (conn!= null) conn.close();
}
}
private String formatResult(ResultSet rset) throws SQLException {
StringBuffer sb = new StringBuffer();
if (!rset.next())
sb.append("<P> No matching rows.<P>\n");
else { sb.append("<UL><B>");
do { sb.append("<LI>" + rset.getString(1) +
" earns $ " + rset.getInt(2) + ".</LI>\n");
} while (rset.next());
sb.append("</B></UL>");
}
return sb.toString();
}
%>
The error occurs on lines:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection((String)session.getAttribute("url"),
"username", "pw");
where session throws an exception. Note that in the oracle code getValue is used instead of getAttribute, but get Value is depreciated. I've tried using a Class.forName() statement; however, this does not seem to solve the problem. Is there anything I have overlooked in this code? Any help would be greatly appreciated. Im using ojdbc14.jar package.
Edited to include error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 28 in the jsp file: /PGS/JDBCQuery.jsp
session cannot be resolved
25: ResultSet rset = null;
26: try {
27: DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
28: conn = DriverManager.getConnection((String)session.getAttribute ("url"),
29: "username", "pw");
30: stmt = conn.createStatement();
31: rset = stmt.executeQuery ("SELECT ename, sal FROM scott.emp"+
Upvotes: 1
Views: 976
Reputation: 429
The problem has nothing to do with the driver.
Al your data logic is inside a JSP declaration tag (<%! %>). The code inside that will be added to the servlet that will be generated from your code. In that context, the session variable doesn't exist.
What you are trying to accomplish would work if you changed the declaration (<%! %>) to a simple scriplet (<% %>) because in that context the session variable does exist and would work as you would expect.
But, as everyone told you already, you shouldn't be adding scriptlets to your jsp pages. Use a servlet with a RequestDispatcher to the jsp file and avoid adding any kind of java code into your jsp files.
Upvotes: 3
Reputation: 691635
The problem has nothing to do with the driver. Your code doesn't compile, because you're defining a method which uses a variable session
which doesn't exist. It's not a local variable nor an argument of your method, and it's not a field of your JSP either.
My advice: stop using scriptlets, and put this code in a Java class. JSPs are view components. they should only be used to generate markup, using the JSP EL, the JSTL and other custom tags. Data access code should be in separate classes, called from a controller implemented as a servlet.
Upvotes: 4