我不明白为什么它不起作用。 它的 Java 插入按钮。错误出现在 InputStream 行中。添加了 2 个进口。流导入 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 block 更新为:
catch (SQLException | FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
关于Java 插入按钮,将 MySQL 添加到数据库和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251232/