Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 4DBC063362A for ; Wed, 3 Jun 2009 16:25:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 00692-02 for ; Wed, 3 Jun 2009 16:25:19 -0300 (ADT) X-Greylist: delayed 00:12:40.171312 by SQLgrey-1.7.6 Received: from n6a.bullet.mail.ac4.yahoo.com (n6a.bullet.mail.ac4.yahoo.com [76.13.13.69]) by mail.postgresql.org (Postfix) with SMTP id D77D2632FFA for ; Wed, 3 Jun 2009 16:25:21 -0300 (ADT) Received: from [76.13.13.26] by n6.bullet.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:12:40 -0000 Received: from [76.13.10.176] by t3.bullet.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:12:40 -0000 Received: from [127.0.0.1] by omp117.mail.ac4.yahoo.com with NNFMP; 03 Jun 2009 19:12:40 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 845574.36185.bm@omp117.mail.ac4.yahoo.com Received: (qmail 20165 invoked by uid 60001); 3 Jun 2009 19:12:40 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1244056360; bh=8iufQJ0N92dC9MPgkDr4FuL1gK44JMpceKaQ55dK6ew=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type; b=NgOLFVONS5LTa8djYOGc3+f7/shRsO2B9dk3jZ4rL6YM3jONr1mVPI9B0ahA71fiswMy8r3BnjIg4Qyzl19k/0d03oencuO0RyWBQ4XDZWoGm4eWer+O8AIzPsob5TGPwgVfirX8RpGB7tCPQLjfuuNrVf62hEkz189/zDtdOtg= 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:Reply-To:Subject:To:MIME-Version:Content-Type; b=YXn76fwdX3wn18O8tqTEXM+3tLo4LyD704FL4VkuwYGBwhQPP/9vIoEGbYGbhH6o+79mYGmSTWIK7LzkgCu6D9kGBIIeSeDN54Tdt5YSid12rTJXiSUZZjaj7/UZKI6X2X2rGlp1F8wMA6LblMRwszjf1idHDoPn4p5rNmaA//s=; Message-ID: <626803.19538.qm@web59808.mail.ac4.yahoo.com> X-YMail-OSG: nVinRX0VM1noKJXoDzpuVotN7Y3mE9.de6BuJCzZidy.Wg2_8UfUsjCogmmuv8IPDIE_IXcO8pUqfsP2PBU0P4mlDE9TuJgv7GVIUaXeTl926kBcm1B.NKAiZ3FxmP7wnnJnfYBboipJWf_Q0t.VR7F9Uke8PT4T4H6b2mg.EHicxzZHt2sJuEWIK6lwvky46msTFThaxU.2x1runYOgdHVjse5EFpqCIvQBq.xDc5rpp_TpmQJqDmhKbgVUQLYpJDmKAMefNfeFWbooZrI- Received: from [207.58.254.162] by web59808.mail.ac4.yahoo.com via HTTP; Wed, 03 Jun 2009 12:12:40 PDT X-Mailer: YahooMailClassic/5.3.9 YahooMailWebService/0.7.289.10 Date: Wed, 3 Jun 2009 12:12:40 -0700 (PDT) From: user2037@ymail.com Reply-To: user2037@ymail.com Subject: 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? The attached test can work with Mysql and their JDBC driver. 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/15 X-Sequence-Number: 19679 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); } }