Re: JDBC to load UTF8@psql to latin1@mysql

From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To:
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: JDBC to load UTF8@psql to latin1@mysql
Date: 2012-12-12 18:35:05
Message-ID: CA+6hpanNSxiWXhjAg7Hheds+n8C0_hT+_GSXbdxge7SdHsasVw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I don't think your Java code does what you think it does. You should read
some more about how Java handles string encodings. Here is a method I wrote
some years ago that might also help you. It converts streams, not strings,
but what you need should be pretty close (and simpler):

/**
* Interprets in according to encIn, and converts it to encOut,
* writing to out. Allocates buffer for the buffer size.
*
* @param encIn The input encoding.
* @param encOut The output encoding.
* @param in The data to convert.
* @param out Where to send the converted data.
* @param buffer The size of the buffer or 0 for the default.
*
* @throws IOException
*/
public void run(String encIn, String encOut, InputStream in, OutputStream
out, int buffer) throws IOException {
Reader r = null;
Writer w = null;
int len;
char[] b;

try {
if (buffer > 0) {
r = new BufferedReader(new InputStreamReader(in, encIn), buffer);
w = new BufferedWriter(new OutputStreamWriter(out, encOut), buffer);
} else {
r = new BufferedReader(new InputStreamReader(in, encIn));
w = new BufferedWriter(new OutputStreamWriter(out, encOut));
buffer = DEFAULT_BUFFER_SIZE;
}
b = new char[buffer];

while ((len = r.read(b, 0, buffer)) != -1) {
w.write(b, 0, len);
}
} finally {
try {
if (r != null) r.close();
} finally {
if (w != null) w.close();
}
}
}

Btw, none of this has anything to do with Postgres. :-)

Paul

On Wed, Dec 12, 2012 at 10:19 AM, Emi Lu <emilu(at)encs(dot)concordia(dot)ca> wrote:

>
> Is there a simple way to load UTF8 data in psql to mysql(with latin1
>> encoding) through JDBC?
>>
>> JAVA codes work for most of characters, but not "-È". Someone knows why
> the following codes cannot load "-È" to mysql(at)latin1?
>
> Thanks a lot!
>
> --
> public static String utf8_to_latin1(String str)
> throws Exception
> {
> try
> {
> String stringToConvert = str;
> byte[] convertStringToByte = stringToConvert.getBytes("UTF-**8");
> return new String(convertStringToByte, "ISO-8859-1");
> }catch(Exception e)
> {
> log.error("utf8_to_latin1 Error: " + e.getMessage());
> log.error(e);
> throw e;
>
> }
> }
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-general<http://www.postgresql.org/mailpref/pgsql-general>
>

--
_________________________________
Pulchritudo splendor veritatis.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-12-12 21:12:01 Re: Columns defined not matching pg_attibute
Previous Message Sergey Konoplev 2012-12-12 18:24:42 Re: NOTIFY/LISTEN on server, asynchronous processing