运行界面如下:
当输入要查找的分数段时可以正常运行(此次输入的为50-80分数段,班级号为空)
当不输入分数段而输入班级号时,系统会报错(此次只输入了班级号,分数段为空)
图中显示执行了上面的分数段,但代码中的条件判断为if( score1!=null&&score2!=null),既然没有进行输入,为何或执行其中的代码而报错呢,后面经过多次试验发现,分数段的text不能为空时才会正常执行,其他情况下都会报错,所以不知道这其中出现了什么问题,求各位大佬解答。
源码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import = "java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="frm" method="post" action="selectDemo.jsp">
输入你需要查找的分数段:<input type="text" name="score1">
<input type="text" name="score2"><br><br>
输入需要查找的班级号:<input type="text" name="clazz"><br>
<input type="submit" value="搜索"onclick = "window.location = selectDemo.jsp">
<hr>
</form>
<%
request.setCharacterEncoding("UTF-8");
String score1=request.getParameter("score1");
String score2=request.getParameter("score2");
String clazz=request.getParameter("clazz");
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:D:/students","sa","123");
Statement stat = conn.createStatement();
if(clazz!=null)
{
String sql="select * from student where clazz='"+clazz+"'";
ResultSet rs1 = stat.executeQuery(sql);
while(rs1.next()){
out.print(rs1.getString(1)+" ");
out.print(rs1.getString(2)+" ");
out.print(rs1.getString(3)+" ");
out.print(rs1.getString(4)+"<br>");
}
}
if( score1!=null&&score2!=null)
{
String sql="select * from student where score>="+score1+"and score<="+score2+"order by score asc";
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
out.print(rs.getString(1)+" ");
out.print(rs.getString(2)+" ");
out.print(rs.getString(3)+" ");
out.print(rs.getString(4)+"<br>");
}
}
%>
</body>
</html>
数据库截图: