Last active
August 29, 2015 14:05
-
-
Save aemxn/74fecc990a8b483c1c13 to your computer and use it in GitHub Desktop.
No search result fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getSearchResult(String search) { | |
StringBuilder buf = new StringBuilder(); | |
String searchQuery = "SELECT * FROM data_structure WHERE element_name LIKE '%" + search + "%'"; | |
// String searchQuery = "SELECT * FROM data_structure WHERE data_structure_type LIKE '%" + search + "%' or element_name LIKE '%" + search + "%' or element_description LIKE '%" + search + "%' or rational LIKE '%" + search + "%' or field_namecode LIKE '%" + search + "%' or alias LIKE '%" + search + "%' or field_size LIKE '%" + search + "%' or element_owner LIKE '%" + search + "%'"; | |
try { | |
currentCon = ConnectionManager.getConnection(); | |
stmt = currentCon.createStatement(); | |
rs = stmt.executeQuery(searchQuery); | |
buf.append("<h3>Search Result for <strong>" + search + "</strong></h3>"); | |
// returns true if there are rows in ResultSet | |
while (rs.next()) { | |
buf.append("<p><strong>Data Structure Type:</strong> " + rs.getString("data_structure_type") + "</br>"); | |
buf.append("<strong>Element Name:</strong> " + rs.getString("element_name") + "</br>"); | |
buf.append("<strong>Element Description:</strong> " + rs.getString("element_description") + "</br>"); | |
buf.append("<strong>Field Namecode:</strong> <code>" + rs.getString("field_namecode") + "</code></br>"); | |
buf.append("<strong>Alias:</strong> " + rs.getString("field_namecode") + "</p><hr/>"); | |
} | |
// No rows found | |
if (!rs.first()){ | |
buf.append("<p><strong>No search result</strong></p>"); | |
} | |
} catch (SQLException e) { | |
System.out.println("Search failed: An Exception has occurred! " + e); | |
} finally { | |
if (rs != null) { | |
try { | |
rs.close(); | |
} catch (SQLException e) { | |
rs = null; | |
} | |
} | |
if (stmt != null) { | |
try { | |
stmt.close(); | |
} catch (SQLException e) { | |
rs = null; | |
} | |
} | |
if (currentCon != null) { | |
try { | |
currentCon.close(); | |
} catch (SQLException e) { | |
} | |
currentCon = null; | |
} | |
} | |
return buf.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment