Skip site navigation (1) Skip section navigation (2)

Re: Problem escaping, nonstandard use of \\ in a string literal

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Warren Bell *EXTERN*" <warren(at)clarksnutrition(dot)com>,<pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Problem escaping, nonstandard use of \\ in a string literal
Date: 2008-08-19 06:42:10
Message-ID: D960CB61B694CF459DCFB4B0128514C20267A132@exadv11.host.magwien.gv.at (view raw or flat)
Thread:
Lists: pgsql-jdbc
Warren Bell wrote:
>>> I am having a problem with escaping characters after upgrading to 
>>> 8.3. I have changed the postgresql.conf file to contain:
>>>
>>> standard_conforming_strings = on
>>>
>>> I am now getting the following error:
>>>
>>> syntax error at or near "S" at character 282
>>>
>>> I am not escaping this character in my code. I am assuming that the 
>>> driver is escaping it. I am using the postgresql-8.3-603.jdbc3.jar as 
>>> the driver.
>>
>> Could you send a short code sample for your problem?
>> It would make it easier for me to look.
>   
> I am using Ibatis and the prepared statement that was being used was 
> something like:
> 
> INSERT INTO tabel (somestringfield) VALUES (?)
> 
> the parameter that was passed was 'Joe\'s'. I also tried E'Joe\'s'. It 
> did not like the escaped apostrophe.

The following code runs without problems:

public class WarrenBell {
    public static void main(String[] args) {
        try {
            java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:postgresql:......");
            conn.createStatement().executeUpdate("SET standard_conforming_strings=on");
            java.sql.PreparedStatement stmt = conn.prepareStatement("SELECT * FROM (VALUES(?)) tab(value)");
            stmt.setString(1, "Joe's");
            if (stmt.execute()) {
                java.sql.ResultSet rs = stmt.getResultSet();
                rs.next();
                System.out.println(rs.getString(1));
                rs.close();
            }
            stmt.close();
            conn.close();
        } catch (java.sql.SQLException e) {
            System.err.println("Error " + e.getErrorCode() + " (SQLSTATE "
                    + e.getSQLState() + "): " + e.getMessage());
            e.printStackTrace();
        }
    }
}

It prints the following on standard output:
Joe's

Yours,
Laurenz Albe

In response to

pgsql-jdbc by date

Next:From: Shahaf AbileahDate: 2008-08-20 21:05:43
Subject: Tracing SQL statements per page/request
Previous:From: Oliver JowettDate: 2008-08-19 01:01:48
Subject: Re: Problem escaping, nonstandard use of \\ in a string literal

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group