Re: JDBC driver, PGSQL 7.3.2 and accents characters

From: Daniel Bruce Lynes <dlynes(at)shaw(dot)ca>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: JDBC driver, PGSQL 7.3.2 and accents characters
Date: 2003-03-19 23:11:03
Message-ID: 200303191511.03619.dlynes@shaw.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Wednesday 19 March 2003 01:35, Davide Romanini wrote:

> I've nice problems with the jdbc driver. I've tried with the jdbc2,
> jdbc, latest stable and also development release.
> I've a database in postgres with some varchar fields. The database is
> SQL_ASCII as char encoding. In that varchar fields I've stored also
> names with accents such è, à, ì etc... They work fine using the psql
> program, and also linking tables to access through the odbc driver. But
> when I try to use jdbc to connect to database my accents fail to load.
> For example I have the string 'Forlì Sud'. When I try to
> system.out.println this string catched by jdbc with rs.getString, I see
> this string instead of the original one: 'Forl?ud'.
> I've tried also to use different character sets in the connection url
> like ISO-8859-1, UNICODE, WIN, SQL_ASCII but didn't change anything.
>
> Please help me, because this bug makes java and jdbc pretty unusable to
> connect pgsql databases.

I doubt very much it's a bug in pgsql. It's probably more than likely a
misunderstanding on your part about how character sets work in Java.

I'm guessing Barry Lind didn't read the last part of your message, or he
probably would've known what the problem was, as well.

He is correct however, in stating that PostgreSQL probably will not allow you
to save accented characters in a database with an encoding of SQL_ASCII.
You'll need to use SQL_UNICODE(?) as the encoding, more than likely.

Because your character set is iso-8859-1 however, you'll need to convert the
strings to Unicode first, before saving to the database.

You do this as follows:

byte[] text=myString.getBytes("iso-8859-1") ;
String myNewString=new String(text,"utf-8") ;
stmt.setString(x,myNewString) ;

To get it back out, try the following:

String myString=rs.getString(x) ;
byte[] text=myString.getBytes("utf-8") ;
String myNewString=new String(text,"iso-8859-1") ;

If you want your code to be portable, I should insist on you specifying the
character set every time for getting bytes and creating strings. The reason
being is that different operating environments will have different default
character sets. For instance, in our office, I've got three default
character sets. On one Linux machine, it's ISO-8859-1, on another, it's
GB2312-80, and on the Windows machines it's CP859(?). The codepage in
question on Windows is Microsoftese for ISO-8859-1/Latin 1/US ASCII with
Latin A, depending on which standard you're used to. It's also often
referred to as CP437 (DOS and OS/2).

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message David Hooker 2003-03-20 00:54:50 Problem with updateRow()
Previous Message Barry Lind 2003-03-19 20:18:55 Re: JDBC driver, PGSQL 7.3.2 and accents characters