5

在使用此代码时,它显示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);
   }        
}     

我怎样才能从这个异常中恢复?

4

2 回答 2

2

尝试从 sql 中删除通配符并将其添加到值中:

String sql = "select * from STD where Name like ? ";
pst=conn.prepareStatement(sql);
pst.setString(1,"%"+TXT_STUDENTNAME.getText()+"%");

类似的问题在这里

于 2012-11-10T05:41:23.243 回答
0
private void cmd_searchActionPerformed(java.awt.event.ActionEvent evt) {                                           
try{

 String sql = "select * from STD where Name like '%?%' ";
      String strStudentName = TXT_STUDENTNAME.getText();
      pst=conn.prepareStatement(sql);
      pst.setString(1,strStudentName );
      rs=pst.executeQuery();
       jTable1.setModel(DbUtils.resultSetToTableModel(rs));

             }catch(Exception e){
       JOptionPane.showMessageDialog(null,e);
   }        
}  

我想这会对你有所帮助..

字符串 strStudentName = TXT_STUDENTNAME.getText(); pst.setString(1,strStudentName);

于 2012-11-10T06:27:41.107 回答