如何检查ResultSet是否为空?

时间:2011-04-22 15:08:21

标签: resultset

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%><%@page import = "java.sql.ResultSet"  %>
<%@page import = "java.sql.Connection"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getting Database Connection</title>
</head>
<body>
    <%
     ResultSet result = null ;
   Connection connection1 = null;
  try
  {
      String driverName1 = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driverName1);
      String serverName = "localhost";
      String portNumber = "1521";
      String sid = "XE";
      String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
      String username = "SYSTEM";
      String password = "password";
      connection1 = DriverManager.getConnection(url, username, password);
      out.println(connection1.isClosed());


      String Name = request.getParameter("name");
      String Age = request.getParameter("age");
       int AgeConvert = Integer.parseInt(Age); 
       String ADR = request.getParameter("address");
       String sqlInsert =  " INSERT INTO ADDRESS  (NAME,AGE,ADR)  VALUES ('" + Name + "' , " + AgeConvert + ", '" + ADR +"')";

       Statement stm = connection1.createStatement();
       stm.execute(sqlInsert);
       out.println(" User Created");

  }

catch(Exception e )
{
    out.println("User not created");
    e.printStackTrace();

}
%>
 <table>
  <tr>
  <td> <b>To search the User Please enter the name in the below box </b> </td>
  </tr>
  <tr>
  <td> Name </td>
  <td> <input type = "text" name ="userName" /> </td>
  </tr>
  <tr>
   <td> <input type ="submit" value = "Search" /></td>
   </tr>
  </table>

<%    ResultSet results = null ;
     String userNameToBeSearched = request.getParameter("userName");
     String sqlselect =  " SELECT * FROM ADDRESS WHERE NAME ='"+userNameToBeSearched+"'";
     Statement stm2 = connection1.createStatement();
     results = stm2.execute(sqlselect);
    **// getting error in this loop it says can convert boolean to ResultSet please help me**
     if (results.next()) {  

    do {  

        %>
        <TABLE BORDER="1">
            <TR>
               <TH>Name</TH>
               <TH>Age</TH>
               <TH>Address</TH>
            </TR>
           <TR>
               <TD> <%= results.getString(1) %> </TD>
               <TD> <%= results.getInt(2) %> </TD>
               <TD> <%= results.getString(3) %> </TD>


          </TR>
       </TABLE>

     <%     
      } while (results.next());  

     } else 

        {  
    out.println("User not found");
 }  
     %>

        </body>
</html>

4 个答案:

答案 0 :(得分:2)

 if(!resultSet.isBeforeFirst()){
   System.out.println("resultset contin no rows");
 }
如果光标位于第一行之前,

isBeforeFirst()返回true;如果光标位于任何其他位置或结果集不包含行

,则返回false

答案 1 :(得分:0)

尝试stm2.exexuteQuery()

这将获取结果集

答案 2 :(得分:0)

如果找不到更多行,

ResultSet.next()会返回false,否则会true

答案 3 :(得分:0)

它不会起作用:

if(!resultSet.next()){

  System.out.println("Rows not found");
}

.next()将遍历行并验证是否有任何行。