Search code examples
javasqlnetbeansinsertsqlexception

SQLException: No value specified for parameter 9


Im trying to create an application that allows users to create a profile however when im inserting into the DB I get the error shown above. I've had a look at similiar solutions but nothing seems to have worked.

The relevant code as it stands is;

//Invokes myConnection class to link to DB
    Connection con = myConnection.getConnection();
    PreparedStatement ps;

    try 
    {
        //Adds the selected text to DB
        ps = con.prepareStatement("INSERT INTO `user`(`username`, `realname`, `password`, `email`, `gym`, `belt`, `dateofbirth`, `profilepic`, `biography`, `motto`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        ps.setString(1, jTextFieldUsername.getText());
        ps.setString(2, jTextFieldName.getText());
        ps.setString(3, String.valueOf(jPasswordFieldPass.getPassword()));
        ps.setString(4, jTextFieldEmail.getText());
        ps.setString(5, jTextFieldGym.getText());
        ps.setString(6, jComboBoxBelt.toString());
        ps.setDate(7, convertUtilDateToSqlDate(jDateChooserDOB.getDate()));


        InputStream img = new FileInputStream(new File(imagePath));

        ps.setBlob(8, img);

        if(ps.executeUpdate() != 0)
        {
            JOptionPane.showMessageDialog(null, "Account Created!");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Oops! Something went wrong!");
        }

        ps.setString(9, jTextAreaBiography.getText());
        ps.setString(10, jTextAreaMotto.getText());
    } 
    catch (Exception ex) 
    {
        Logger.getLogger(RegisterPage.class.getName()).log(Level.SEVERE, null, ex);
    }

Sorry if this is straight forward and thanks in advance for your help!

Edit: Answered simply, thanks was having a complete brainfart there.


Solution

  • The problem in your code is,

    You have to set all the values for the parameters and then use execute statement.

    your code should be like this.

    ps = con.prepareStatement("INSERT INTO `user`(`username`, `realname`, `password`, `email`, `gym`, `belt`, `dateofbirth`, `profilepic`, `biography`, `motto`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    ps.setString(1, jTextFieldUsername.getText());
    ps.setString(2, jTextFieldName.getText());
    ps.setString(3, String.valueOf(jPasswordFieldPass.getPassword()));
    ps.setString(4, jTextFieldEmail.getText());
    ps.setString(5, jTextFieldGym.getText());
    ps.setString(6, jComboBoxBelt.toString());
    ps.setDate(7, convertUtilDateToSqlDate(jDateChooserDOB.getDate()));
    
    
    InputStream img = new FileInputStream(new File(imagePath));
    ps.setBlob(8, img);
    ps.setString(9, jTextAreaBiography.getText());
    ps.setString(10, jTextAreaMotto.getText());
    
    if(ps.executeUpdate() != 0)
    {
         JOptionPane.showMessageDialog(null, "Account Created!");
    }
     else
    {
        JOptionPane.showMessageDialog(null, "Oops! Something went wrong!");
    }