首页 > 解决方案 > SQLException: 没有为参数 9 指定值

问题描述

我试图创建一个允许用户创建配置文件的应用程序,但是当我插入数据库时​​,我得到了上面显示的错误。我看过类似的解决方案,但似乎没有任何效果。

现有的相关代码是;

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

对不起,如果这是直截了当的,并提前感谢您的帮助!

编辑:简单地回答,谢谢在那里有一个完整的脑残。

标签: javasqlnetbeansinsertsqlexception

解决方案


您在ps.executeUpdate()没有设置参数 9 和 10 的情况下运行。

将这些行移到之前if(ps.executeUpdate() != 0):-

ps.setString(9, jTextAreaBiography.getText());
ps.setString(10, jTextAreaMotto.getText());

推荐阅读