Java – how to find an executed SQL query that does not return anything?

import java.net.URL;
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);}
}
}

If the query returns one line, the above program will print out If the query does not return anything, the program will stop without printing anything

Instead of stopping it without printing anything, I want the program to recognize that the query does not return any content and print out, as shown below: "no content is returned after the SQL query is executed"

How do I recognize the use of certain methods or variables to execute a query without returning any rows?

Solution

boolean hasRows = false;
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());
}

Remember to check if if (! Rs.next()) advances the cursor to the result set Don't go forward again until you get the value

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>