用的是MyEclipse+Tomcat5.xDB是一个数据库的类<%
Connection conn = DB.getConn();
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
rs.next();
out.println(rs.getString("title"));
DB.close(conn);
%>这样写完全没有问题.运行也正常.------------------------------------------------------------------------------------但是如下改动后<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "";
//如果不写rs.next(),也是没有异常的.
while(rs.next()) {
str = rs.getString("title");
}
return str;
}
%>
<%
Connection conn = DB.getConn();
String str = getTitle(conn);
out.println(str);
DB.close(conn);
%>就会出异常:
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 11 in the jsp file: /article.jsp
Unhandled exception type SQLException
8: Statement stmt = DB.createStmt(conn);
9: ResultSet rs = DB.executeQuery(stmt,sql);
10: String str = "";
11: while(rs.next()) {
12: str = rs.getString("title");
13: }
14: return str;--------------------------------------------------------------------------------------也就是说,上面这段出错的代码,如果注释掉那个rs.next(),就不会出异常,即
<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "111";
//while(rs.next()) {
// str = rs.getString("title");
//}
//注释这句后不会有异常
return str;
}
%>
<%
Connection conn = DB.getConn();
String str = getTitle(conn);
out.println(str);
DB.close(conn);
%>
--------------------------------------------------------------加下rs.next()就会出org.apache.jasper.JasperException: Unable to compile class for JSP:
请问这个是什么原因..刚学JSP...谢谢
Connection conn = DB.getConn();
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
rs.next();
out.println(rs.getString("title"));
DB.close(conn);
%>这样写完全没有问题.运行也正常.------------------------------------------------------------------------------------但是如下改动后<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "";
//如果不写rs.next(),也是没有异常的.
while(rs.next()) {
str = rs.getString("title");
}
return str;
}
%>
<%
Connection conn = DB.getConn();
String str = getTitle(conn);
out.println(str);
DB.close(conn);
%>就会出异常:
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 11 in the jsp file: /article.jsp
Unhandled exception type SQLException
8: Statement stmt = DB.createStmt(conn);
9: ResultSet rs = DB.executeQuery(stmt,sql);
10: String str = "";
11: while(rs.next()) {
12: str = rs.getString("title");
13: }
14: return str;--------------------------------------------------------------------------------------也就是说,上面这段出错的代码,如果注释掉那个rs.next(),就不会出异常,即
<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "111";
//while(rs.next()) {
// str = rs.getString("title");
//}
//注释这句后不会有异常
return str;
}
%>
<%
Connection conn = DB.getConn();
String str = getTitle(conn);
out.println(str);
DB.close(conn);
%>
--------------------------------------------------------------加下rs.next()就会出org.apache.jasper.JasperException: Unable to compile class for JSP:
请问这个是什么原因..刚学JSP...谢谢
9: ResultSet rs = DB.executeQuery(stmt,sql);
10: String str = "";
11: while(rs.hasNext()) {
rs.next();
12: str = rs.getString("title");
13: }
14: return str;
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "";
//如果不写rs.next(),也是没有异常的.
while(rs.next()) {
str = rs.getString("title");
}
return str;
}
%>
<%
Connection conn = DB.getConn();
String str = getTitle(conn);
out.println(str);
DB.close(conn);
%>
将那个红色的叹号去掉就可以了。
<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
try{
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "111";
//while(rs.next()) {
// str = rs.getString("title");
//}
//注释这句后不会有异常
}catch(Exception e){
....一些处理
}
return str;
}
%>
<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
try{
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "111";
//while(rs.next()) {
// str = rs.getString("title");
//}
//注释这句后不会有异常
}catch(Exception e){
....一些处理
}
return str;
}
%>
rs好象没有hasNext()这方法..而且下列代码是没有异常的
<%
Connection conn = DB.getConn();
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
rs.next();
out.println(rs.getString("title"));
DB.close(conn);
%>
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
try {
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
String str = "111";
rs.next();
str = rs.getString("title");
} catch (SQLException e) {
e.printStackTrace();
}
return str;
}
%>这样写依然没用.出现的异常是
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 17 in the jsp file: /article.jsp
str cannot be resolved
14: } catch (SQLException e) {
15: e.printStackTrace();
16: }
17: return str;
18: }
19: %>
20:
------------------------------------------------------------问题是为什么这样写就没有异常呢??效果一样,而把rs.next写到getTitle中就出错了,其他的我调试了在getTitle都没有问题
<%
Connection conn = DB.getConn();
String sql = "select * from article where pid = 1";
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
rs.next();
out.println(rs.getString("title"));
DB.close(conn);
%>
放到大括号外面:
<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
String str = "111";
try {
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql); rs.next();
str = rs.getString("title");
} catch (SQLException e) {
e.printStackTrace();
}
return str;
}
%>
ResultSet rs
这些变量都放到外面,在finally语句中进行一下释放:<%!
private String getTitle(Connection conn) {
String sql = "select * from article where pid = 1";
String str = "111";
Statement stmt = null;
ResultSet rs = null;try {
stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
rs.next();
str = rs.getString("title");
} catch (SQLException e) {
e.printStackTrace();
}
finally{
//将rs、stmt、conn释放
}return str;
}
%>