带有MySQL的Java插入按钮添加到数据库和表



我不明白为什么它不起作用。 它的Java插入按钮。错误位于输入流行中。增加了 2 个进口。Stream import java.io.FileInputStream;导入java.io.InputStream;

InputStream img = new FileInputStream(new File(ImgPath((;错误 未报告的异常 FileNotFoundException;必须捕获或声明

private void Btn_InsertActionPerformed(java.awt.event.ActionEvent evt) {                                           
if (checkInputs() && ImgPath != null) {
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO products(name,price,add_date,image"
+ "value(?,?,?,?) ");
ps.setString(1, "txt_name.getText()");
ps.setString(2, "txt_price.getText()");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format("txt_AddDate.getDate()");
ps.setString(3, addDate);
InputStream img = new FileInputStream(new File(ImgPath));
ps.setBlob(4, img);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data ");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
else {
JOptionPane.showMessageDialog(null, "One or More Filed Are Empty");
}
} 

将 catch 块更新为:

catch (SQLException | FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}

最新更新