私人無效btnInsertActionPerformed(EVT java.awt.event.ActionEvent中){Java錯誤:可以多抓或幾個catch子句捕捉特定異常
if(checkInputs() && imgPath != null){
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO animal_information(animal_breed,animal_age,animal_found,animal_hairColor,"
+ "animal_eyeColor,animal_specialTrait,animal_codeNumber,animal_image)"
+ "values(?,?,?,?,?,?,?)");
//ps.setString(1, pawsId.getText());
ps.setString(1, pawsBreed.getText());
ps.setString(2, pawsAge.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(pawsDate.getDate());
ps.setString(3, addDate);
ps.setString(4, pawsHairColor.getText());
ps.setString(5, pawsEyeColor.getText());
ps.setString(6, pawsUniqueTrait.getText());
InputStream img = new FileInputStream(new File(imgPath));
ps.setBlob(7, img);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Inserted Data Successfully");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
else{
JOptionPane.showMessageDialog(null, "One or More Fields Are Empty");
}
System.out.println("Breed => "+ pawsBreed.getText());
System.out.println("Age => "+ pawsAge.getText());
System.out.println("Hair Color => "+ pawsHairColor.getText());
System.out.println("Eye Color => "+ pawsEyeColor.getText());
System.out.println("Unique Traits => "+ pawsUniqueTrait.getText());
System.out.println("Image => "+ imgPath);
}
好傢伙更換!我只是一名學生,完全是初學者學習java。我遇到了這個問題「可以用多捕獲或幾個捕獲子句捕捉特定的異常」在部分catch(Exception ex)的圖像中它有黃色下劃線顏色,這就給我一個錯誤。我想插入數據到我的數據庫,但它沒有插入,因爲這個錯誤。正如你可以在我的代碼中看到的,我試圖打印數據,它都是成功的。有人可以請啓發和幫助我,我做錯了什麼。請...
發佈文字;代碼是文本。不要讓人們看圖像。無論如何,立即捕捉所有異常通常被認爲是不好的做法。這不是一個錯誤,只是一個壞主意。 –
你可以在帖子中添加代碼,而不是像img? – Bob
對不起,關於圖片,我現在編輯它,代碼已發佈。 – missNobody