我有這樣的代碼剪斷,我用輸入驗證:如何比較字符串和對象在Java中
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));
}
}
出於某種原因,輸入數據不正確地在Oracle中的值進行比較。比較兩個值的正確方法是什麼?
「未正確比較」 - 請更具描述性。你使用了什麼樣的輸入,結果是什麼? –
我沒有得到任何輸出。正確的輸出應該是「..會話ID已經在使用!」 – user1285928