Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException Parameter index out of range

While am using this code it shows 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);
   }        
}     

How can i recover from this exception?

like image 709
Abhishek Mohandas Avatar asked Nov 03 '22 11:11

Abhishek Mohandas


1 Answers

Try to remove the wildcard from the sql and add it to the value:

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

Similar question here

like image 81
iTech Avatar answered Nov 08 '22 04:11

iTech