我试图创建一个允许用户创建配置文件的应用程序,但是当我将其插入数据库时,出现了上面显示的错误。我看过类似的解决方案,但似乎没有任何效果。
目前的相关代码是
//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);
}
很抱歉,如果这样很简单,请先感谢您的帮助!
编辑:简单地回答,谢谢你在那里有个完整的脑袋。
答案 0 :(得分:1)
您正在运行ps.executeUpdate()
,而未设置参数9和10。
将这些行移动到if(ps.executeUpdate() != 0)
之前:-
ps.setString(9, jTextAreaBiography.getText());
ps.setString(10, jTextAreaMotto.getText());
答案 1 :(得分:1)
您的代码中的问题是
您必须设置参数的所有值,然后使用execute语句。
您的代码应该是这样的。
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!");
}