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...
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())是否会将光标前进到结果集中.在获得值之前不要再次前进.
编程基础网
本文标题为:java – 如何找到执行的SQL查询没有返回任何内容?
基础教程推荐
猜你喜欢
- SpringBoot测试配置属性与web启动环境超详细图解 2023-06-18
- 一文掌握Java中的JWT 2022-12-07
- Go Java算法之简化路径实例详解 2023-05-14
- SpringBoot+Thymeleaf实现生成PDF文档 2023-05-18
- 用法介绍Java HelloWorld程序 2023-10-08
- Java8中Stream的详细使用方法大全 2023-02-04
- Java中比较Long类型是否相等 2023-10-08
- Spring AOP底层原理及代理模式 2022-11-28
- java版spring cloud+spring boot+redis社交电子商务平台(十)使用Swagger2构建强大的RESTful API文档(2) 2023-10-29
- java – 在JOOQ自定义绑定中生成SQL以访问MySQL JSON函数时访问字段名称或别名 2023-10-29
