[英]Java skips else condition
我不確定為什么,但是由於某些原因,以下代碼跳過了else
條件。 我已經嘗試了幾乎所有我能想到的東西,包括切換代碼塊,但它仍然跳過了else
部分。 基本上,如果在FILESTATUS
數據庫中找不到傳遞給該方法的String docID
,則我希望此方法返回String temp = "no"
如果找到,則FILESTATUS
String temp = "yes"
。
static String checkDocID(String docID)
{
String temp = null;
System.out.println("Checking if data already exists in database...");
try
{
Main.stmt = Main.con.createStatement();
String command = "SELECT * FROM FILESTATUS WHERE ID='" + docID + "'";
ResultSet queryResult = Main.stmt.executeQuery(command);
if (!queryResult.next())
{
temp = "no";
}
else
{
while (queryResult.next())
{
String result = queryResult.getString("ID");
if (result.equals(docID))
{
temp = "yes";
break;
}
}
}
Main.stmt.close();
}
catch (Exception ex) {ex.printStackTrace();}
return temp;
}
謝謝!
因為最終您在“ if”中調用了queryResult.next(),並在一段時間內再次調用,所以跳過了第一個結果。 如果只有一個結果,則while循環將永遠不會執行。
如果我可以提出幾點建議:
result.equals(docID)
因為查詢已經確定了這一點。 最好將此循環重組為:
static String checkDocId(String docId) {
String temp = "no";
while (queryResult.next()) {
String result = queryResult.getString("ID");
if (result.equals(docID)) {
temp = "yes";
break;
}
}
return temp;
}
有些人不喜歡使用break
(我通常不喜歡),因此您可以在一段時間內使用布爾值(我發現它的讀起來更像英語,並且您可以從while
就告訴終止條件, while
不用尋找if
內):
static String checkDocId(String docId) {
boolean found = false;
while (queryResult.next() && !found) {
String result = queryResult.getString("ID");
found = result.equals(docID);
}
return found ? "yes" : "no";
}
否則,您將執行不必要的比較。 請記住, while
僅僅是一個if
有goto
末;)
就您的問題而言,保羅所說的是正確的。 無論如何,我仍將重組循環,以使其更優雅。
在對代碼有點驚訝之后(泄漏了數據庫資源,不信任數據庫它返回了正確的docID
,在完全不需要所有列的情況下執行了SELECT *
,沒有利用SQL注入消毒功能的PreparedStatement
,使用String
代替boolean
表示真/假值,一片狼藉碼流),這里是如何真正應該做,而不是:
private static final String SQL_EXIST_DOCID =
"SELECT id FROM filestatus WHERE id = ?";
public boolean exist(String docID) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean exist = false;
try {
connection = database.getConnection();
statement = connection.prepareStatement(SQL_EXIST_DOCID);
statement.setString(1, docID); // Shouldn't it be a Long instead? Characterbased PK's are *far* from efficient.
resultSet = statement.executeQuery();
exist = resultSet.next();
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return exist;
}
如此清晰,簡潔,簡單。 有關以正確方式使用基本JDBC的更多見解,您可能會發現本文有用。 希望您認真學習。 真。
rs.next()在每次調用后滾動當前光標
嘗試
static String checkDocID(String docID)
{
String temp = null;
System.out.println("Checking if data already exists in database...");
try
{
Main.stmt = Main.con.createStatement();
String command = "SELECT * FROM FILESTATUS WHERE ID='" + docID + "'";
ResultSet queryResult = Main.stmt.executeQuery(command);
boolean found = queryResult.next();
if (!found)
{
temp = "no";
}
else
{
do {
String result = queryResult.getString("ID");
if (result.equals(docID))
{
temp = "yes";
break;
}
} while (queryResult.next())
}
Main.stmt.close();
}
catch (Exception ex) {ex.printStackTrace();}
return temp;
}
在嘗試讀取queryResult.next之前,您要調用它兩次。 為了證明這一點,將println或其他內容放在其他位置之后(之前)。
由於您是通過特定的ID選擇的,因此兩次移動到next()實際上將第二次失敗(大概是),並且永遠不會執行while塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.