5

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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);
   }        
}  

I think this will help you..

String strStudentName = TXT_STUDENTNAME.getText(); pst.setString(1,strStudentName );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.