I have this code snipped which I use for input validation:
public void validaUserID(FacesContext context, UIComponent component, Object value) throws ValidatorException, SQLException {
int findAccount = 0;
if (ds == null) {
throw new SQLException("Can't get data source");
}
// Initialize a connection to Oracle
Connection conn = ds.getConnection();
if (conn == null) {
throw new SQLException("Can't get database connection");
}
// Convert Object into String
int findValue = Integer.parseInt(value.toString());
// With SQL statement get all settings and values
PreparedStatement ps = conn.prepareStatement("SELECT * from USERS where USERID = ?");
ps.setInt(1, findValue);
try {
//get data from database
ResultSet result = ps.executeQuery();
while (result.next()) {
// Put the the data from Oracle into Hash Map
findAccount = result.getInt("USERID");
}
} finally {
ps.close();
conn.close();
}
// Compare the value from the user input and the Oracle data
if (value.equals(findAccount)) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
value + " Session ID is already in use!", null));
}
}
For some reason the input data is not compared properly with the value in Oracle. What is the proper way to compare the two values?
It looks like you’re comparing boxed integers. I’d unwrap them (i.e. get them in primitive form) and do
==instead of.equals.