ResultSet 返回值出现异常:
下面是我的javabean和对应的jsp页面:
package demo;
import java.sql.*;
public class ContactBean2 {
private Connection con;
private Statement stmt;
private ResultSet avgRs;
private String strAvg;
public ContactBean2() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;Databasename=bb",
"sa", "aa");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFound!");
} catch (SQLException e) {
System.out.println("SQL Server Not Connection");
}
}
public ResultSet getAvgData(int startCaoh, int endCaoh, String date) {
try {
strAvg = "select avg(wend) as wend from gongyirpt where riq='"
+ date + "' and caoh>='" + startCaoh + "' and caoh <='"
+ endCaoh + "'";
stmt = con.createStatement();
avgRs = stmt.executeQuery(strAvg);
return avgRs;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
test.jsp如下:
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312"%>
<jsp:useBean id="dbc" class="demo.ContactBean2" scope="page"> </jsp:useBean>
<html>
<body>
<table>
<tr>
<td>日平均 </td>
<%ResultSet rsAvg1 = null;
try {
rsAvg1 =dbc.getAvgData(1001, 1037, "2007-11-14");
if (rsAvg1.next()) {%>
<td> <%=Double.parseDouble(rsAvg1.getString("wend"))%> </td>
</tr> <%} else {%> <td>0 </td> </tr> <%}
} catch (SQLException e) {
e.printStackTrace();
}
rsAvg1.close();
dbc.closeData();%>
</table>
</body>
</html>
现在的问题是如果我出入的日期是"2007-11-14",就会返回正确结果,因为这个时候数据中有符合条件的记录,但是如果是其他日期,也就是说在数据库中没有符合条件的记录,就会出现错误,出错的意思应该是 <%=Double.parseDouble(rsAvg1.getString("wend"))%>这一句,请大家给看看错在什么地方啊,如果该应该怎么该,先谢谢啊。。。。。。。。在线等拉。。。。。。。
------解决方法--------------------------------------------------------
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312"%>
<jsp:useBean id="dbc" class="demo.ContactBean2" scope="page"> </jsp:useBean>
<html>
<body>
<table>
<tr>
<td> 日平均 </td>
<%ResultSet rsAvg1 = null;
try {
rsAvg1 =dbc.getAvgData(1001, 1037, "2007-11-14");
while(rsAvg1.next()) {
if(rsAvg1.getString("wend")!=null){
%>
<td> <%=Double.parseDouble(rsAvg1.getString("wend"))%> </td>
</tr> <%
} else {
%> <td> 0 </td> </tr> <%
}
}
} catch (SQLException e) {
e.printStackTrace();
rsAvg1.close();
}
rsAvg1.close();
//dbc.closeData();
%>
</table>
</body>
</html>
在那做个判断就行了。。。。