[英]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.