【问题标题】:What has changed between postgres jdbc 8.4 and 9 regarding bytearrays?关于字节数组,postgres jdbc 8.4 和 9 之间发生了什么变化?
【发布时间】:2011-01-28 12:35:13
【问题描述】:

我正在运行带有 PostgreSQL 9.0 的 Mac OSX 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 " +

            }

这是事实。

  1. 使用驱动程序 postgresql-9.0-801.jdbc4.jar 它 完美地工作在 PostgreSQL 8.4 和 PostgreSQL 9

  2. 使用驱动程序 8.4-701.jdbc4 有效 仅在 PostgreSQL 8.4 中。

  3. 使用 带有 PostgreSQL 的驱动程序 8.4-701.jdbc4 9 不工作。提取的文件不同。一个 md5 表明数据库中的内容与原始文件相同。因此,我的假设是问题出在文件的提取过程中。

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

【问题讨论】:

    标签: java postgresql jdbc


    【解决方案1】:

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

    查看发行说明:
    http://www.postgresql.org/docs/9.0/static/release-9-0.html#AEN99255

    详细配置设置说明:
    http://www.postgresql.org/docs/9.0/static/runtime-config-client.html#GUC-BYTEA-OUTPUT

    【讨论】:

    • +1 tks 为您解答!总之,驱动程序 8.4 期望以转义模式发送数据,但 PostgreSQL 9 以十六进制模式发送数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多