繁体   English   中英

我怎么知道从Java到SQL Server的SQL选择查询没有返回任何结果?

[英]How can I know if a SQL select query from Java to SQL Server didn't return any results?

我想知道从Java到SQL Server的SELECT查询是否不返回任何结果,因为我试图在数据库中进行“验证”以了解User已经存在,因此是否返回了SELECT查询例如false ,我知道给定的user不存在,可以将其添加到数据库中。

Connection c = ConexionSQL.Conexion.getConection();
try {
    Statement select = c.createStatement();
    ResultSet r = select.executeQuery("SELECT * FROM Usuario WHERE id="+id);

    if(r.next()) {
        //Show message that the user already Exists
    }
    else {
        //Add user to the database
    }
}
catch(Exception ex) {
    System.out.println("No se puedo realizar el select");
    ex.printStackTrace();
}

我猜测,你的代码的工作,但不是使用r.next()您可以使用r.isBeforeFirst()因为如果有数据,你就不必走回头路把它找回来。

总而言之,如果光标不在第一行之前或没有行,则isBeforeFirst()返回false。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM