1 Reply Latest reply: May 3, 2008 11:38 PM by 3004 RSS

    jdbc select statement

    807601
      Hi,

      I got :
      String unitNum="select * from unitList where unitNum='"+unitNum_txt+"';";  
      //unitList is the table,search field name is unitNum
      
                  try{
                       Statement stmt=con.createStatement();
                      ResultSet rs = stmt.executeQuery(unitNum);  
                     while(rs.next()){
                          System.out.println(rs.getString(1)+"\t-"+rs.getString(2)+"\t-"+rs.getString(3)+"\t-"+rs.getString(4));
                      }
                     
                  }
      If the unitList table has the record..the details are printed out.But if the table doesn't have it,what code do i write to detect it?In my sql ,the statement will return empty set.

      what java code would detect ,no record found?

      Hope someone could guide me out.

      Rahul
        • 1. Re: jdbc select statement
          3004
          If there are no rows returned, the body of the while loop will never be entered. You don't need to do anything special.

          If you wnat to explicitly note that no rows were returned, you could do something like this:
          boolean rowsReturned = false;
          
          while (rs.next()) {
            rowsReturned = true;
            // process current row
          }
          
          if (rowsReturned) {
            // rows were returned
          }
          else {
            // no rows were returned
          }