How to find that a executed SQL query returns nothing?

import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
public class searchlink{
public static void main(String args[]) throws Exception {
    //String link="http://hosted.ap.org";
    Connection con=null;
    Statement stmt=null;
    Statement stmtR=null;
    if(con==null){
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con=SQLConnection.getNewConnection();
            stmt=con.createStatement();
            stmtR=con.createStatement();
    }
    ResultSet rs;
    rs=stmt.executeQuery("select url from urls where url='http://www.topix.com/rty/elyria-oh'");
    while(rs.next()){
    String mem=rs.getString(1);
    System.out.println("Result is "+mem);}
}
}

The above program displays the result if the query returns a string. If the request returns nothing, the program stops without printing anything.

Instead of stopping without printing, I want the program to identify that the query did not return anything and print an output saying something like this: "After the SQL query is executed, nothing is returned."

How to determine, using any method or variable, that the request was executed without returning a string?

+3
source share
8 answers
boolean hasRows = false;
while(rs.next()){
  hasRows = true;
  // do other stuff required.
}

if(!hasRows)
{
  // do stuff when no rows present.
}

- or -

if(!rs.next())
{
  // do stuff when no rows prsent.
}
else
{
  do{
  // do stuff required
  }while(rs.next());
}

bearing in mind that checking if (! rs.next ()) will advance the cursor in the result set. Do not increase it until you get the values.

+11

JDBC- - List<Entity>. , try-with-resources statement, . - , . , .

:

public List<Entity> list() throws SQLException {
    List<Entity> entities = new ArrayList<Entity>();

    try (
        Connection connection = database.getConnection();
        PreparedStatement statement = connection.prepareStatement("SELECT id, name, value FROM entity");
        ResultSet resultSet = statement.executeQuery();
    ) {
        while (resultSet.next()) {
            Entity entity = new Entity(); 
            entity.setId(resultSet.getLong("id"));
            entity.setName(resultSet.getString("name"));
            entity.setValue(resultSet.getInteger("value"));
            entities.add(entity);
        }
    }

    return entities;
}

List :

List<Entity> entities = entityDAO.list();

if (entities.isEmpty()) {
    // It is empty!
}
else if (entities.size() == 1) {
    // It has only one row!
}
else {
    // It has more than one row!
}

. :

+5
if (rs.hasNext())
{
    while(rs.next())
   {
    String mem=rs.getString(1);
    System.out.println("Result is "+mem);
   }

}
else
{
   System.out.println("There is nothing returned after SQL query execution ");
}

~

+2

(! Rs.isBeforeFirst())  System.out.println( " " );

+1

(rs.next()) , - . , ( ).

, , , "if" "where".

   . . .


   ResultSet rs;
   rs=stmt.executeQuery("select url from urls where url='http://www.topix.com/rty/elyria-oh'");
   if (rs.next() {
      printRow(rs);
      while(rs.next()){
          printRow(rs);
      }
    }
    else {
        System.out.println("no data returned");
    }
  }

  static public printRow(ResultSet rs) {
    String mem=rs.getString(1);
    System.out.println("Result is "+mem);}
  }   

}
0

...

int count = 0;

while ( rs.next() )
{
    count++;

    String mem=rs.getString(1);   
    System.out.println("Result is "+mem);}
    .
    .
    .
}

...

if (count==0)
{
    // show your message "There is nothing returned after SQL query execution"
}

rs.next() , if (rs.next() == false) , 2 , , .

,

Rick

0
boolean got_result = false;
while (...) {
  got_result = true;
  ...
}
if (!got_result) {
  ...
}
0

for selected queries you can do

rs.next();
int value = resultSet.getInt(1);
if (value == 0)
{
        //throw error message
}
else
        // 
0
source

Source: https://habr.com/ru/post/1760594/


All Articles