I'm trying to insert data using a UI I've been trying to develop. I tried to update all the data accordingly but somehow I got the error 'No value specified at parameter 8'. From the other questions I saw in this site, some of them were because of them specifying wrong number of parameters considering the number of their '?' written within their program. I couldn't see why mine would act like that as well.
Please help.
private void Btn_InsertActionPerformed(java.awt.event.ActionEvent evt) {
if ((checkInputs() && ImgPath!= null) || (checkInputs() && ImgPath2!= null)){
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO players(name, designation, team, exp, age, price, imagefront, imageoptional" + "values(?, ? , ?, ?, ?, ?, ?, ?) )");
ps.setString(1, txt_Name.getText());
ps.setString(2, txt_Designation.getText());
ps.setString(3, txt_Team.getText());
ps.setString(4, txt_Exp.getText());
ps.setString(5, txt_Age.getText());
ps.setString(6, txt_Price.getText());
InputStream img1 = new FileInputStream(new File(ImgPath));
ps.setBlob(7, img1);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Inserted");
InputStream img2 = new FileInputStream (new File (ImgPath2));
ps.setBlob(8, img2);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Inserted");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
else {
JOptionPane.showMessageDialog(null, "One or More Field Are Empty");
}
I suspect that there must be something wrong with the Image uploading method, but I couldn't see what it was either. These lines was the one used for ImgPath
:
private void Btn_Choose_Image_FrontActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.image", "jpg", "png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION){
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
lbl_imageFront.setIcon(ResizeImage1(path,null));
ImgPath = path;
}
else {
System.out.println("No File Selected");
}
}
And the next one, for the 'Optional Image', which is ImgPath2
:
private void Btn_Choose_Image_OptionalActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file2 = new JFileChooser();
file2.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter2 = new FileNameExtensionFilter("*.image","jpg","png");
file2.addChoosableFileFilter(filter2);
int result2 = file2.showSaveDialog(null);
if (result2 == JFileChooser.APPROVE_OPTION){
File selectedFile2 = file2.getSelectedFile();
String path2 = selectedFile2.getAbsolutePath();
lbl_imageOptional.setIcon(ResizeImage2(path2,null));
ImgPath2 = path2;
}
}
I'm still quite new to Java and MySQL especially, the two images slot in the table were declared as LONGBLOB type, because I believe that is the correct one. Please help. Thanks in advance.
Comments
Post a Comment