Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id D92B5634E1F for ; Wed, 3 Jun 2009 16:25:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39088-09 for ; Wed, 3 Jun 2009 16:25:38 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from n3a.bullet.mail.ac4.yahoo.com (n3a.bullet.mail.ac4.yahoo.com [76.13.13.66]) by mail.postgresql.org (Postfix) with SMTP id 14D84634A63 for ; Wed, 3 Jun 2009 16:25:37 -0300 (ADT) Received: from [76.13.13.25] by n3.bullet.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:25:36 -0000 Received: from [76.13.10.184] by t4.bullet.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:25:36 -0000 Received: from [127.0.0.1] by omp125.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:25:36 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 789053.26895.bm@omp125.mail.ac4.yahoo.com Received: (qmail 12068 invoked by uid 60001); 3 Jun 2009 19:25:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1244057136; bh=8iufQJ0N92dC9MPgkDr4FuL1gK44JMpceKaQ55dK6ew=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=BSFAjO/Gs7gCt2OWnufqbK7opHQj+yjDHuH0ernk2MYSdg2x7lDE1pEoDKVELBimeHLL8K0o44qPgLU0IDfWLCH8pzU0bmflPpEkuiXf4yuiZotlA7qP8l6gJ89LnvUZG86+17/a9gp2DztwTFRRE8wPswbKb0Ml43eGX94WFzc= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=ymail.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=gTu2P4XK6I42PLzNwCi9HoFc7zpVzo2BVBhNX2hpaYBZceISRsT0fM10X/1wd2gOlgy0FKtYmMU2uMXNr1FPuQZ+GEmpMxQiTzb6o+yToUp4UXfIq3ixh2g9i9y4D4fqPLioetYGBDIJpglmuMyGFmTQwUheMRkE08MTVGH7GSw=; Message-ID: <548495.696.qm@web59815.mail.ac4.yahoo.com> X-YMail-OSG: Jz90LQgVM1n1nFJmLJc5anVyAO9dWyH7vpfoxv4CBrRfdPpj1.YTolFq1FWwPpGJAQivg8YDSS83PZkRjFcVKlsMWsJmJ0iTIsWv2wOxWnj9glQ8mY_DRiA0mKVVnnnFMCVTgZi3PXcaYrnvXdG0Czv6a02FfqwB6HJx99daBCrfvX0JGFTvxI0Shs30VzcCrIEneSzHPgoHn1hdjDUzNqulkNdmUCVlWZDVQ1c5I79YXxk68y93gwSBXI7GmBBgfT8hK3q5fLwKU5LBjsk- Received: from [207.58.254.162] by web59815.mail.ac4.yahoo.com via HTTP; Wed, 03 Jun 2009 12:25:36 PDT X-Mailer: YahooMailClassic/5.3.9 YahooMailWebService/0.7.289.10 Date: Wed, 3 Jun 2009 12:25:36 -0700 (PDT) From: User Subject: Strings with Null Bytes Causing Exceptions To: pgsql-jdbc@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200906/16 X-Sequence-Number: 19680 Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? It can work with Mysql and their JDBC driver. import java.sql.*; public class TestStringsWithNullBytes { public static void main(String[] args) { String noNulls = "No null bytes"; String nulls = "Null bytes \000\000"; try { Class.forName("org.postgresql.Driver"); Connection db = DriverManager.getConnection( "jdbc:postgresql://localhost/test?user=test&password=secret"); /* Class.forName("com.mysql.jdbc.Driver"); Connection db = DriverManager.getConnection( "jdbc:mysql://localhost/test?user=test&password=secret"); */ db.setAutoCommit(true); tryQuery(db, "SELECT * FROM test"); tryQuery(db, "DROP TABLE IF EXISTS test"); tryQuery(db, "CREATE TABLE test (name TEXT)"); tryQuery(db, "SELECT '" + noNulls + "'"); tryQuery(db, "SELECT '" + nulls + "'"); tryQuery(db, "INSERT INTO test (name) VALUES('" + noNulls + "')"); tryQuery(db, "INSERT INTO test (name) VALUES('" + nulls + "')"); tryPreparedQuery(db, "SELECT ?", noNulls); tryPreparedQuery(db, "SELECT ?", nulls); tryPreparedQuery(db, "INSERT INTO test (name) VALUES(?)", noNulls); tryPreparedQuery(db, "INSERT INTO test (name) VALUES(?)", nulls); tryQuery(db, "SELECT * FROM test"); } catch(Exception e) { e.printStackTrace(); } } private static void tryQuery(Connection db, String query) { try { Statement s = db.createStatement(); if (s.execute(query)) { ResultSet rs = s.getResultSet(); while(rs.next()) { System.out.println(escape(query) + ": " + escape(rs.getString(1))); } } } catch(Exception e) { System.err.println(escape(query) + ": " + e.getMessage()); } } private static void tryPreparedQuery(Connection db, String query, String s) { try { PreparedStatement ps = db.prepareStatement(query); ps.setString(1, s); if (ps.execute()) { ResultSet rs = ps.getResultSet(); while(rs.next()) { System.out.println(escape(query) + ": " + escape(rs.getString(1))); } } } catch(Exception e) { System.err.println(escape(query) + ": " + e.getMessage()); } } private static StringBuilder escape(String s) { StringBuilder sb = new StringBuilder(); for(int i = 0; i < s.length(); i++) { if ((32 > (int)s.charAt(i)) || (126 < (int)s.charAt(i))) { sb.append("<" + (int)s.charAt(i) + ">"); } else { sb.append(s.charAt(i)); } } return(sb); } }