使用根本原因刷新数据时发生异常java.io.IOException:Stream已关闭

时间:2018-06-13 11:57:54

标签: html jsp

我想从数据库中检索数据,其中用户名=在登录期间由用户输入。 用户在html中输入用户名 但是我得到的错误是:在根据原因刷新数据时出现异常 java.io.IOException:Stream已关闭。  使用根本原因刷新数据时发生异常  小溪关闭。

我的jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Details</title>
</head>
<body>
<%

String uname= request.getParameter("UserName");
try{


    Class.forName("com.mysql.jdbc.Driver");  
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas","root","rass");  
    PreparedStatement ps=con.prepareStatement("select * from Student_Data where UserName="+uname);

    ResultSet rs = ps.executeQuery();

%>


<table rules="all" border="">
<%

while (rs.next()) {
%>
<tr>
<td>FirstName: </td><td><%out.println(rs.getString(1));%></td></tr>
<td>LastName: </td><td><%out.println(rs.getString(2));%></td></tr>
<tr><td>UserName: </td><td><%out.println(rs.getString(3));%></td></tr>
<tr><td>Password: </td><td><%out.println(rs.getString(4));%></td></tr>
<tr><td>DateOfBirth: </td><td><%out.println(rs.getString(5));%></td></tr>
<tr><td>E-Mail</td><td><%out.println(rs.getString(6));%></td></tr>
<tr><td>Mobile.No</td><td><%out.println(rs.getString(7));%></td></tr>
<tr><td>Gender</td><td><%out.println(rs.getString(8));%></td></tr>
<tr><td>Address</td><td><%out.println(rs.getString(9));%></td></tr>
<tr><td>City</td><td><%out.println(rs.getString(10));%></td></tr>
<tr><td>PinCode</td><td><%out.println(rs.getString(11));%></td></tr>
<tr><td>State</td><td><%out.println(rs.getString(12));%></td></tr>
<tr><td>Country</td><td><%out.println(rs.getString(13));%></td></tr>
<tr>
<td>Hobbies</td>
<td><%out.println(rs.getString(14));%> </td>
</tr>
<tr>
<td>Qualification</td><td>Sl.No</td><td>Examination</td><td>Board</td><td>Percentage</td><td>Year of Passing</td>
</tr>       
<tr>
<td></td><td>1</td><td>Class X</td><td><%out.println(rs.getString(15));%></td><td><%out.println(rs.getString(16));%></td><td><%out.println(rs.getString(17));%></td>
</tr>
<tr>
<td></td><td>2</td><td>Class XII</td><td><%out.println(rs.getString(18));%></td><td><%out.println(rs.getString(19));%></td><td><%out.println(rs.getString(20));%></td>
</tr>
<tr>
<td></td><td>3</td><td>Graduation</td><td><%out.println(rs.getString(21));%></td><td><%out.println(rs.getString(22));%></td><td><%out.println(rs.getString(23));%></td>
</tr>
<tr>
<td>Course Applied</td><td><%out.println(rs.getString(24));%>
</td>
</tr>

</table>


<%
}
}
catch(Exception e)
{
    System.out.println(e);
}
out.close();
%>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您不应该在jsp页面中调用out.close,jsp会为您执行此操作

所以只需删除以下代码:

<%
}
}
//remove below code
catch(Exception e)
{
    System.out.println(e);
}
out.close();
%>

对于您的查询sql,因为您使用了PreparedStatement,所以您需要传递参数ps.setString()而不是直接在sql中写入,因此请更改为以下内容:

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas","root","rass");  
PreparedStatement ps=con.prepareStatement("select * from Student_Data where UserName=?");
ps.setString(1,uname);