java – 如何找到执行的SQL查询没有返回任何内容?

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);}
}
}
如果查询返回一行,上面的程序将打印输出.
如果查询未返回任何内容,程序将停止而不打印任何内容.
而不是在没有打印任何东西的情况下停止它,我希望程序识别出查询没有返回任何内容并打印输出,如下所示“SQL查询执行后没有返回任何内容”.
如何识别使用某些方法或变量来执行查询而不返回任何行?
解决方法:
boolean hasRows = false;
while(rs.next()){
hasRows = true;
// do other stuff required.
}
if(!hasRows)
{
// do stuff when no rows present.
}
– 要么 –
if(!rs.next())
{
// do stuff when no rows prsent.
}
else
{
do{
// do stuff required
}while(rs.next());
}
请记住检查if(!rs.next())是否会将光标前进到结果集中.在获得值之前不要再次前进.