如何检查ResultSet是否为空?

发布于 2024-11-03 10:32:12 字数 3143 浏览 4 评论 0原文

<%@ 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>
<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只有一腔孤勇 2024-11-10 10:32:12
 if(!resultSet.isBeforeFirst()){
   System.out.println("resultset contin no rows");
 }

如果光标位于第一行之前,isBeforeFirst() 返回 true;如果光标位于任何其他位置或结果集不包含行,则为 false

 if(!resultSet.isBeforeFirst()){
   System.out.println("resultset contin no rows");
 }

isBeforeFirst() returns true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows

海未深 2024-11-10 10:32:12

尝试 stm2.exexuteQuery()

这将获取结果集

try stm2.exexuteQuery()

This will grab the result set

江湖彼岸 2024-11-10 10:32:12

如果没有找到更多行,ResultSet.next() 返回 false,否则返回 true

ResultSet.next() returns false if no more rows are found, otherwise true.

路弥 2024-11-10 10:32:12

这样做不行吗:

if(!resultSet.next()){

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

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

Would it not work doing:

if(!resultSet.next()){

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

.next() will go through the rows and validate if there is any row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文