为了防止 sql 注入,我试图避免使用WHERE Surname LIKE '"search_txt.getText()"'
... 但是使用 apreparedStatement
正在离开 ? 空白,换句话说,即使在文本字段中输入了正确的搜索值,sql语句也无法执行...行中的问题是什么pst1.setString(1, search_txt.getText()+"%");
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
try{
sql = "SELECT Title, Forename, Surname, Role FROM AcademicInfo WHERE Surname LIKE ? OR Forename LIKE ?";
PreparedStatement pst1 = conn.prepareStatement(sql);
ResultSet rs2 = pst1.executeQuery();
if (rs2.next()) {
pst1.setString(1, search_txt.getText()+"%");
pst1.setString(2, search_txt.getText()+"%");
System.out.println(sql);
JOptionPane.showMessageDialog(null, "Found Academic");
Update_table(sql);
} else {
System.out.println(sql);
JOptionPane.showMessageDialog(null, "No Results Found");
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}