29 lines
1.1 KiB
Plaintext
29 lines
1.1 KiB
Plaintext
== Parameterized Queries – Java Example
|
||
[source,java]
|
||
-------------------------------------------------------
|
||
public static String loadAccount() {
|
||
// Parser returns only valid string data
|
||
String accountID = getParser().getStringParameter(ACCT_ID, "");
|
||
String data = null;
|
||
String query = "SELECT FIRST_NAME, LAST_NAME, ACCT_ID, BALANCE FROM USER_DATA WHERE ACCT_ID = ?";
|
||
try (Connection connection = null;
|
||
PreparedStatement statement = connection.prepareStatement(query)) {
|
||
statement.setString(1, accountID);
|
||
ResultSet results = statement.executeQuery();
|
||
if (results != null && results.first()) {
|
||
results.last(); // Only one record should be returned for this query
|
||
if (results.getRow() <= 2) {
|
||
data = processAccount(results);
|
||
} else {
|
||
// Handle the error – Database integrity issue
|
||
}
|
||
} else {
|
||
// Handle the error – no records found }
|
||
}
|
||
} catch (SQLException sqle) {
|
||
// Log and handle the SQL Exception }
|
||
}
|
||
return data;
|
||
}
|
||
-------------------------------------------------------
|