当我使用这段代码时,它显示 java.sql.SQLException Parameter index out of range (1 > number of parameters, which is 0)
:
private void cmd_searchActionPerformed(java.awt.event.ActionEvent evt) {
try{
String sql = "select * from STD where Name like '%?%' ";
pst=conn.prepareStatement(sql);
pst.setString(1,TXT_STUDENTNAME.getText());
String value=TXT_STUDENTNAME.getText();
rs=pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
如何从这个异常中恢复?
最佳答案
尝试从sql中删除通配符并将其添加到值中:
String sql = "select * from STD where Name like ? ";
pst=conn.prepareStatement(sql);
pst.setString(1,"%"+TXT_STUDENTNAME.getText()+"%");
关于java.sql.SQLException 参数索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13319481/