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