我使用以下查询来检查数据库中是否已存在问题。
SELECT * FROM QUESTBANK where QUESTION = ?;
以下是我使用上述查询的代码
public static boolean authQuestion(String question) throws SQLException {
boolean isQuestionAvailable = false;
Connection dbCon = null;
try {
dbCon = MyResource.getConnection();
String query = "SELECT * FROM QUESTBANK where QUESTION = ?;";
PreparedStatement checkStmt = dbCon.prepareStatement(query);
checkStmt.setString(1, question);
ResultSet rs = checkStmt.executeQuery();
while (rs.next()) {
isQuestionAvailable = true;
}
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (Exception e) {
if (dbCon != null)
dbCon.close();
} finally {
if (dbCon != null)
dbCon.close();
}
return isQuestionAvailable;
}
问题在于,如果问题文本完全匹配,则通过。但是,如果用户添加额外字符并尝试将其添加到db,则会添加该字符。如何通过优化查询来有效防止添加此类重复记录?当然问题可能是多线的