我正在尝试将图像保存在 postgresql 表中
//table schema
CREATE TABLE public.hp_xray_results
(
patient_no character varying(70),
xray_image bytea
)
java.sql.PreparedStatement pstmt = connectDB.prepareStatement("INSERT INTO hp_xray_results (patient_no,xray_image) VALUES(?,?)");
pstmt.setString(1, jTextField36.getText());
File file = new File(String.valueOf(jTable3.getValueAt(i, 4)));
FileInputStream fis=null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
Exceptions.printStackTrace(ex);
}
pstmt.setBinaryStream(2, fis, (int) file.length());
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
Exceptions.printStackTrace(ex);
}
pstmt.executeUpdate();
运行上面的代码时,出现异常。 org.postgresql.util.PSQLException:无法绑定(bind)语句的参数值。
我可以做出哪些改进?
最佳答案
将所有代码替换为以下内容:
File file = new File(...);
String sql = "INSERT INTO hp_xray_results (patient_no,xray_image) VALUES(?,?)";
try (PreparedStatement pstmt = connectDB.prepareStatement(sql);
FileInputStream fis = new FileInputStream(file)) {
pstmt.setString(1, jTextField36.getText());
pstmt.setBinaryStream(2, fis, (int) file.length());
pstmt.executeUpdate();
} catch (Exception e) {
...
}
文档链接:
关于java - 将图像保存到 Postgresql Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41611138/