早上好,
这是我的 JSP 代码(注意,它只是来自 Oracle 示例应用程序页面的稍微修改的源代码: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();
}
%>
错误发生在以下行:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection((String)session.getAttribute("url"),
"username", "pw");
session 抛出异常的地方。请注意,在 oracle 代码中使用 getValue 而不是 getAttribute,但 get Value 是贬值的。我试过使用 Class.forName() 语句;但是,这似乎并不能解决问题。这段代码有什么我忽略的吗?任何帮助将不胜感激。我使用 ojdbc14.jar 包。
编辑包括错误:
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"+