Reputation: 1068
I don't need the if after its condition has been met. Is there any way I can modify my code so that it doesn't have to check for it after it passed? I have a lot of tables in my database and I'm wondering if the code is optimal.
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM STACKOVERFLOW.information_schema.tables ORDER BY TABLE_NAME");
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
ExtractFrom.addItem(name);
if (name.toLowerCase().equals("stack")) pvIsPresent=true;
}
if (pvIsPresent)
ExtractFrom.setSelectedItem("stack");
Upvotes: 0
Views: 77
Reputation: 191
More ugly and more simply maybe but you can put a counter before while as
and in the if statement you can ask as
than after if statement you can increment the counter as
than since the counter never become 0 again the if statement never be checked again.
I hope it helps too.
Upvotes: 0
Reputation: 1002
You could change the while statement to this:
while(rs.next && !pvIsPresent)...
Upvotes: 0
Reputation: 13349
This is somehow ugly but...
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM STACKOVERFLOW.information_schema.tables ORDER BY TABLE_NAME");
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
ExtractFrom.addItem(name);
if (name.toLowerCase().equals("stack")) {
pvIsPresent = true;
break;
}
}
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
ExtractFrom.addItem(name);
}
if (pvIsPresent)
ExtractFrom.setSelectedItem("stack");
Upvotes: 1
Reputation: 198033
You could just do if (!pvIsPresent && name.toLowerCase().equals("stack"))
.
Although you might also want to use the slightly more efficient name.equalsIgnoreCase("stack")
.
Upvotes: 5