结果集rs无法解析

时间:2017-04-22 08:01:09

标签: java servlets

最近我开始学习Java技术现在我正在研究Servlets,而我从数据库中获取数据它没有解析结果集rs 在这里我附上了我的代码

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">    
javascript:window.history.forward(1);
</script>

<%  
response.setHeader("Pragma","no-cache");  
response.setHeader("Cache-Control","no-store");  
response.setHeader("Expires","0");  
response.setDateHeader("Expires",-1);  
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="update.jsp">update</a>
<a href="delete.jsp">display</a> 
<a href="logout.jsp">logout</a> 
<script>
    <%
    try{
        ResultSet rs=null;
        String mail=(String)session.getAttribute("mail");
        String sql="select * from tempregister where mail=?";
        Class.forName("com.ibm.db2.DB2Driver");
        Connection con=DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
        PreparedStatement ps=con.prepareStatement(sql);
    ps.setString(1, mail);
    rs=ps.executeQuery();


    }catch(Exception e){

    }
    %>


   <% 
   while(rs.next())
   {

   %>
<%=rs.getString(4)%>


<%
   }
}
catch(Exception e)
{
e.printStackTrace();
}%>

</script>
</a> 


</body>
</html>

请帮帮我..谢谢

1 个答案:

答案 0 :(得分:1)

您的代码至少应该更改:

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<script
type="text/javascript" language="javascript">    
javascript:window.history.forward(1);
</script>

<%  
response.setHeader("Pragma","no-cache");  
response.setHeader("Cache-Control","no-store");  
response.setHeader("Expires","0");  
response.setDateHeader("Expires",-1);  
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="update.jsp">update</a>
<a href="delete.jsp">display</a> 
<a href="logout.jsp">logout</a> 
<%
     ResultSet rs = null;
     try {
         String mail = (String) session.getAttribute("mail");
         String sql = "select * from tempregister where mail=?";
         Class.forName("com.ibm.db2.DB2Driver");
         Connection con = DriverManager.getConnection("jdbc:db2://localhost:50000/TEMP", "tarun", "12347890");
         PreparedStatement ps = con.prepareStatement(sql);
         ps.setString(1, mail);
         rs = ps.executeQuery();
         while (rs.next()) {
             %>            
             <%=rs.getString(4)%>
             <%
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
 %>

</body>
</html>