java.sql.SQLException: No value specified for parameter 5. update database

I have this error code in the dialog box: java.sql.SQLException: No value specified for parameter 5 when i try to update my JTable/JTextFields into my SQL database.

I have checked similar questions on the site, but non seem to have the solution to my problem. I have checked the database, i have checked my connection code, the update code and can’t find where this extra parameter making the problem should be? Please help a new beginner!

So now i understand that the problem is at WHERE id=? as i suspected, but my id only exist as a row count/main key in my SQL DB, so it is going to be different depending on which row you choose/click on, so i can’t set a specific value beforehand at the pst.setInt(5, ? ). What to insert instead then – so i dont lose the automatic row count on my clients list in the JTable?

//This method contains all codes for database connection.
    private void upDateDB() {
    try { 
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/num klienter", "root", "");
    PreparedStatement pst = con.prepareStatement("SELECT * FROM klient");
        
    ResultSet rs =pst.executeQuery();
    ResultSetMetaData StData = rs.getMetaData();
        
    q = StData.getColumnCount();
        
    DefaultTableModel RecordTable = (DefaultTableModel)table.getModel();
    RecordTable.setRowCount(0);
        
    while(rs.next()){
       Vector<String> columnData = new Vector<String>();
            
    for (i = 1; i <= q; i++) {
       columnData.add(rs.getString("id"));
       columnData.add(rs.getString("Name"));
       columnData.add(rs.getString("Birthday"));
       columnData.add(rs.getString("Description"));
       columnData.add(rs.getString("Other"));
}
       RecordTable.addRow(columnData);                
            
 }} catch (Exception ex) {
       JOptionPane.showMessageDialog(null, ex);
}}

updateButton.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent arg0) { 
            
        try { 
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/num klienter", "root", "");    
            PreparedStatement pst = con.prepareStatement("UPDATE klient SET Name=?,Birthday=?,Description=?,Other=? WHERE id=?");
            table.getSelectedRow();
        
            pst.setString(1, nameTxt.getText()); 
            pst.setString(2, dayTxt.getText()+"-" + monthTxt.getText()+"-" + yearTxt.getText());
            pst.setString(3, descriptionTxt.getText());
            pst.setString(4, otherTxt.getText());
            pst.executeUpdate();
            
            JOptionPane.showMessageDialog(null, "Updated in database");
            upDateDB();
            
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null, ex); 
}

Answer

The solution for my problem was to insert the 5th pst. statement for the id=? like this:

pst.setInt(5,table.getRowCount());
            
            
            

Leave a Reply

Your email address will not be published. Required fields are marked *