将图像保存到Postgresql Java
我正在尝试在postgresql表中保存图像将图像保存到Postgresql Java,java,image,postgresql,Java,Image,Postgresql,我正在尝试在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) V
//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:无法为语句绑定参数值。
我可以做哪些改进?用以下代码替换所有代码:
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) {
...
}
文档链接: