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
Java PostgresJDBC8.4和9之间关于ByteArray的变化是什么?_Java_Postgresql_Jdbc - Fatal编程技术网

Java PostgresJDBC8.4和9之间关于ByteArray的变化是什么?

Java PostgresJDBC8.4和9之间关于ByteArray的变化是什么?,java,postgresql,jdbc,Java,Postgresql,Jdbc,我正在用PostgreSQL 9.0运行MacOSX 10.6。我编写了一个简单的Java应用程序,在bytea字段中插入一个图像,然后查询同一字段以检查它 下表: CREATE TABLE test.test_table ( id integer NOT NULL, image bytea, CONSTRAINT test_table_pkey PRIMARY KEY (id) ); 该计划类似于: //insert the file PreparedState

我正在用PostgreSQL 9.0运行MacOSX 10.6。我编写了一个简单的Java应用程序,在bytea字段中插入一个图像,然后查询同一字段以检查它

下表:

 CREATE TABLE test.test_table
   (
   id integer NOT NULL,
  image bytea,
  CONSTRAINT test_table_pkey PRIMARY KEY (id)
);
该计划类似于:

//insert the file
    PreparedStatement ps = connection.prepareStatement("INSERT INTO test.test_table( id, image ) VALUES (?, ?);");
            byte[] bytesFromFile = readFile("img/test1.bmp");
            ps.setInt(1, 1);
            ps.setBytes(2, bytesFromFile);
            ps.execute();
            ps.close();

            PreparedStatement stmt = connection.prepareStatement("Select id,image from test.test_table");
            ResultSet rs = stmt.executeQuery();
    //Get the file from the BD  and save it to the FS 
            while (rs.next()) {
                String id = rs.getString(1);
                InputStream imageStream = rs.getBinaryStream(2);
                String imageName = OUTPUT_DIR + "/" + id + ".bmp";
                FileOutputStream f = new FileOutputStream(imageName);
                byte buff[] = new byte[1024];
                int l;
                while ((l = imageStream.read(buff)) > 0) {
                    f.write(buff, 0, l);
                }
                f.close();
                System.out.println("CREATED : " + imageName);// + " size " +

            }
以下是事实

  • 使用驱动程序 postgresql-9.0-801.jdbc4.jar it 在两种情况下都能完美工作 PostgreSQL 8.4和PostgreSQL 9

  • 使用驱动程序8.4-701.jdbc4可以工作 仅在PostgreSQL 8.4中

  • 使用 带PostgreSQL的驱动程序8.4-701.jdbc4 9不起作用。提取的文件不同。md5显示数据库中的内容等于原始文件。因此,我的假设是,问题出在提取文件的过程中

  • 我可以升级驱动程序,没问题。我担心的是:PostgreSQL 9不再支持的通信协议内部发生了什么变化?

    字节数组的编码(服务器发送它们的方式)已从8.4更改为9.0:

    请参阅发行说明:

    以及配置设置的详细说明:

    +1谢谢你的回答!总之,驱动程序8.4希望数据处于转义模式,但PostgreSQL 9以十六进制模式发送数据。