简体   繁体   English

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

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

I am trying to know if a SELECT query made from Java to SQL Server doesn't return any results, because I'm trying to do a "verification" in the database to know if a User already exists, so if the SELECT query returned false , for example, I know that the given user doesn't exist and I can add it to the database. 我想知道从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();
}

I am guessing that your code works, but instead of using r.next() you can use r.isBeforeFirst() , because if there's data, you won't have to backtrack to get it back. 我猜测,你的代码的工作,但不是使用r.next()您可以使用r.isBeforeFirst()因为如果有数据,你就不必走回头路把它找回来。

In summary, isBeforeFirst() returns false if there cursor is not before the first row or if there are no rows. 总而言之,如果光标不在第一行之前或没有行,则isBeforeFirst()返回false。

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

相关问题 Java的SQL Server查询没有结果 - SQL Server query from Java gets no results 如何将Java中的SQL查询结果打印到每列的各个JTextField? - How can I print results from a SQL query in Java to individual JTextFields for each column? SQL查询在Selenium没有返回结果如何处理IndexOutofBounException? - How to handle IndexOutofBounException if the SQL query does not return any results in Selenium? 无法通过我的选择查询从SQL Server获取数据(使用jdbc驱动程序) - Can't get data from a SQL Server with my select query (using jdbc driver) 如何使用Java Servlet输出SQL查询的结果 - How do I output results of an SQL query using java servlet 如何在不安装任何服务器的情况下将我的 java 应用程序连接到本地文件。sql - how can i connect my java app to a local file .sql without install any server 如何将 sql 查询的结果导入 JTable? - How can I make import the results of an sql query into a JTable? java sql 查询我如何 select 没有参数的所有数据 - java sql query how can i select all data without parameter SQL 服务器——如何保证查询结果被缓存? - SQL Server - how to ensure query results are cached? 我无法使用 Oracle Sql 查询在 java 中设置参数 - I can't set parameters in java using Oracle Sql query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM