在postgresql数据库中插入图像

时间:2014-06-04 06:50:00

标签: postgresql jdbc

我已经看过很多关于这个主题的话题,但它不起作用。

这是我的代码:

public void storeChart(JFreeChart chart, String x, String y, String comments) throws SQLException, IOException {
    modifySequenceTable("resultimage");
    //Recovery the last id simulation
    String query_select = "SELECT id FROM simulation";
    ResultSet res = state.executeQuery(query_select);
    res.last();
    int id_fk = res.getInt("id");
    String query_nb_run = "SELECT count(*) FROM resultimage WHERE simulation_id="+id_fk;
    ResultSet res_nb_run = state.executeQuery(query_nb_run);
    res_nb_run.last();
    int nb_run = res_nb_run.getInt("count");
    Object[] p = ExtractParamSimulationGroup();
    String name = p[0]+"_run"+nb_run;

    File image = new File("..\\Chart.png");
    FileInputStream imagestream = new FileInputStream(image);
    String query_insert = "INSERT INTO resultimage (name, x, y, comments, simulation_id, image) ";
           query_insert += "VALUES (?,?,?,?,?,?)"; 


    PreparedStatement ps = conn.prepareStatement(query_insert);
    ps.setString(1, name);
    ps.setBinaryStream(6, imagestream, (int)image.length());
    ps.setString(2, x);
    ps.setString(3, y);
    ps.setString(4, comments);
    ps.setInt(5, id_fk);
    ps.executeUpdate();
    ps.close();

}

没有例外,但在图像列中没有插入任何内容。

这是表格的声明:

CREATE TABLE resultimage
(
  id bigint NOT NULL DEFAULT nextval('hibernate_sequence'::regclass), 
  name character varying(255), 
  image bytea, 
  x character varying(255), 
  y character varying(255), 
  comments text, 
  simulation_id integer NOT NULL, 
  CONSTRAINT resultimage_pkey PRIMARY KEY (id), 
  CONSTRAINT resultimage_fk FOREIGN KEY (simulation_id) REFERENCES simulation (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE
 ) WITH (OIDS=FALSE ); 
 ALTER TABLE resultimage OWNER TO websimulator;

谢谢你

0 个答案:

没有答案