2012-11-10 20 views
5

Mentre sto usando questo codice mostra java.sql.SQLException Parameter index out of range (1 > number of parameters, which is 0):indice java.sql.SQLException Parametro fuori intervallo

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

Come posso recuperare da questa eccezione?

risposta

2

tenta di rimuovere il carattere jolly da SQL e inserirlo al valore:

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

Similar question here

+0

thankz maaan itz in corso ... –

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

penso che questo vi aiuterà a ..

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

Problemi correlati