pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: [JDBC] Using Postgres with Latin1 (ISO8859-1)
3+ messages / 2 participants
[nested] [flat]

* Re: [JDBC] Using Postgres with Latin1 (ISO8859-1)
@ 2004-11-08 21:12 J. Michael Crawford <jmichael@gwi.net>
  2004-11-08 21:55 ` Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 3+ messages in thread

From: J. Michael Crawford @ 2004-11-08 21:12 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-general@postgresql.org; pgsql-jdbc

<<This is bad advice for a Java client and does not work.>>

   Well then, perhaps we shouldn't share the procedure with other folks.  I 
apologize if I'm introducing some misinformation.

   However, this has been the only way to get our system to work on more 
than one JVM.  People from this group provided many suggestions, people 
from other groups did the same, and nothing helped.  Taking bytes and 
translating encodings (examples follow my signature below) had no 
effect.  Changing the url of the postgres connection to include an encoding 
also had no effect.  Setting the encoding for the entire JVM didn't work 
either.  Either the data worked in a Linux VM, or a Windows VM, but not both.

   So, if you're going to correct us for the wrong solution (which I'm glad 
you have done), do you have any suggestions as to what the right solution 
might be?

	- Mike


Encoding translations that didn't work:

a) Getting encoded bytes from the result set.  We tried the following block 
five times, once for each different encoding we were trying to test with 
the database:

dataRead = new String(result.getBytes(longName),"utf-8");
dataLatin_a = new String(dataRead.getBytes("ISO-8859-1"));
dataLatin_b = new String(dataRead.getBytes("Latin1"));
dataUnicode_a = new String(dataRead.getBytes("utf-8"));
dataUnicode_b = new String(dataRead.getBytes("UTF8"));
dataWin = new String(dataRead.getBytes("Cp1252"));

b)  Getting a string, turning it bytes, and then translating.  Same process 
as above, but we use result.getString...

   No matter  what, strings showed up as gibberish in one JVM or another, 
depending upon the native encoding of the database.  A Latin1 database 
worked in the windows JVM, a Unicode in the Linux JVM, but not the other 
way around.





At 12:15 PM 11/8/2004, Kris Jurka wrote:
 >
 >
 >On Mon, 8 Nov 2004, J. Michael Crawford wrote:
 >>
 >>    Even in Java, where you can do all sorts of character-encoding
 >> translation, it can be impossible to translate data retrieved from 
Postgres
 >> if it's in the wrong encoding.  We've tried changing the JVM encoding,
 >> altering the jdbc driver, translating encodings on the database read, and
 >> translating encodings after the read while building a new string, to no
 >> avail.  We tried 25 combinations of each strategy (five different possible
 >> read encodings and five different possible string encodings), and nothing
 >> worked.  We could get an application working in one JVM with one encoding,
 >> but another JVM would break, and no amount of translation would help.
 >>
 >>    But when we finally told Postgres what to return, everythign worked 
like
 >> a charm.
 >>
 >>    Just as with step two, the key is to use the "SET CLIENT_ENCODING TO
 >> (encoding)" sql command.  If you're using an application where you can 
send
 >> SQL to the server, this is all you need.  In something like MS Access,
 >> you'll have to move to a passthrough query.  For Java, you'll need to send
 >> a command through JDBC:
 >>
 >> String DBEncoding = "Unicode"  //use a real encoding, either returned from
 >> the jvm or explicitly stated
 >> PreparedStatement statement = dbCon.prepareStatement("SET CLIENT_ENCODING
 >> TO '" + DBEncoding + "'");
 >> statement.execute();
 >>
 >
 >This is bad advice for a Java client and does not work.  The JDBC driver
 >always expects data in unicode and issues a SET client_encoding of it's
 >own at connection startup to make sure it gets unicode data.  Changing
 >this to another encoding will break the driver and in the cvs version a
 >check has been added to error out if it detects you doing this.
 >
 >Kris Jurka
 >
 >---------------------------(end of broadcast)---------------------------
 >TIP 4: Don't 'kill -9' the postmaster




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [JDBC] Using Postgres with Latin1 (ISO8859-1)
  2004-11-08 21:12 Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) J. Michael Crawford <jmichael@gwi.net>
@ 2004-11-08 21:55 ` Oliver Jowett <oliver@opencloud.com>
  2004-11-09 00:03   ` Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) J. Michael Crawford <jmichael@gwi.net>
  0 siblings, 1 reply; 3+ messages in thread

From: Oliver Jowett @ 2004-11-08 21:55 UTC (permalink / raw)
  To: J. Michael Crawford <jmichael@gwi.net>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-general@postgresql.org; pgsql-jdbc

J. Michael Crawford wrote:

> Encoding translations that didn't work:
> 
> a) Getting encoded bytes from the result set.  We tried the following 
> block five times, once for each different encoding we were trying to 
> test with the database:

> b)  Getting a string, turning it bytes, and then translating.  Same 
> process as above, but we use result.getString...

>   No matter  what, strings showed up as gibberish in one JVM or another, 
> depending upon the native encoding of the database.  A Latin1 database 
> worked in the windows JVM, a Unicode in the Linux JVM, but not the other 
> way around.

The "right way" is to just use getString() and not do any translation 
yourself. The driver has already done the transcoding from whatever the 
DB encoding is, to the internal UTF-16 string representation. You don't 
need to mess with byte-based representations.

When you then display that string, you will need to use an appropriate 
encoding, obviously..

Can you provide a self-contained testcase that demonstrates getString() 
doing the wrong thing?

-O



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [JDBC] Using Postgres with Latin1 (ISO8859-1)
  2004-11-08 21:12 Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) J. Michael Crawford <jmichael@gwi.net>
  2004-11-08 21:55 ` Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) Oliver Jowett <oliver@opencloud.com>
@ 2004-11-09 00:03   ` J. Michael Crawford <jmichael@gwi.net>
  0 siblings, 0 replies; 3+ messages in thread

From: J. Michael Crawford @ 2004-11-09 00:03 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: Kris Jurka <books@ejurka.com>; pgsql-general@postgresql.org; pgsql-jdbc

<<The "right way" is to just use getString() and not do any translation 
yourself.>>

   That was the first thing we tried.  Sorry I didn't mention it.

   The next step was getting a string, turning it into bytes, and 
translating the bytes.  The third step was getting bytes.  Nothing worked 
in our Java GUI, the console, or the web page returned.

   Maybe it was just something weird in our system.  Perhaps Postgres just 
wanted to send Latin1 characters out of our database, which at the time was 
Latin1.  I don't know.  Whatever it was sending we couldn't use, regardless 
of the translation attempted.  Forcing it to Unicode (improperly) did the 
trick for us.  Now that our database is unicode, I'm not sure we'd get the 
same problem.

   If there's a better way to make sure Postgres is sending out Unicode in 
our JVM session, while simultaneously sending out other encodings to other 
apps that access the database using different encodings, I'm all ears.


<<When you then display that string, you will need to use an appropriate 
encoding, obviously..>>

   Definitely.


<<Can you provide a self-contained testcase that demonstrates getString() 
doing the wrong thing?>>

   I can try.  If I can find a copy of the old Latin1 database, and 
replicate the error, I'll let you know via email.

---

   Thanks for your attention on this, both this time, and back in the 
summer when I was asking questions before we arrived at our "fix".  You and 
Kris really carry this discussion group, and we're all lucky to have you do 
it.

   My goal was to add something to the group so that I could do more than 
just receive help.  I also wanted something in the archives that would help 
"newbie" searchers who haven't yet had to deal with the encoding process in 
a java servlet -- we searched for weeks without finding anything that 
covered all the bases.  If there's a way to remove/correct/comment my 
posting, I'm fine with that.

         - Mike



At 04:55 PM 11/8/2004, Oliver Jowett wrote:
>J. Michael Crawford wrote:
>
>>Encoding translations that didn't work:
>>a) Getting encoded bytes from the result set.  We tried the following 
>>block five times, once for each different encoding we were trying to test 
>>with the database:
>
>>b)  Getting a string, turning it bytes, and then translating.  Same 
>>process as above, but we use result.getString...
>
>>   No matter  what, strings showed up as gibberish in one JVM or another, 
>> depending upon the native encoding of the database.  A Latin1 database 
>> worked in the windows JVM, a Unicode in the Linux JVM, but not the other 
>> way around.
>
>The "right way" is to just use getString() and not do any translation 
>yourself. The driver has already done the transcoding from whatever the DB 
>encoding is, to the internal UTF-16 string representation. You don't need 
>to mess with byte-based representations.
>
>When you then display that string, you will need to use an appropriate 
>encoding, obviously..
>
>Can you provide a self-contained testcase that demonstrates getString() 
>doing the wrong thing?
>
>-O




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-11-09 00:03 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-11-08 21:12 Re: [JDBC] Using Postgres with Latin1 (ISO8859-1) J. Michael Crawford <jmichael@gwi.net>
2004-11-08 21:55 ` Oliver Jowett <oliver@opencloud.com>
2004-11-09 00:03   ` J. Michael Crawford <jmichael@gwi.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox