Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将图像保存到Postgresql Java_Java_Image_Postgresql - Fatal编程技术网

将图像保存到Postgresql Java

将图像保存到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

我正在尝试在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:无法为语句绑定参数值。


我可以做哪些改进?

用以下代码替换所有代码:

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) {
    ...
}
文档链接:


代码中是2,在键入时出错