Re: JDBC to load UTF8@psql to latin1@mysql - Mailing list pgsql-general

From Paul Jungwirth
Subject Re: JDBC to load UTF8@psql to latin1@mysql
Date
Msg-id CA+6hpanNSxiWXhjAg7Hheds+n8C0_hT+_GSXbdxge7SdHsasVw@mail.gmail.com
Whole thread Raw
In response to Re: JDBC to load UTF8@psql to latin1@mysql  (Emi Lu <emilu@encs.concordia.ca>)
Responses Re: JDBC to load UTF8@psql to latin1@mysql  (Emi Lu <emilu@encs.concordia.ca>)
List 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@encs.concordia.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@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@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--
_________________________________
Pulchritudo splendor veritatis.

pgsql-general by date:

Previous
From: Sergey Konoplev
Date:
Subject: Re: NOTIFY/LISTEN on server, asynchronous processing
Next
From: Tom Lane
Date:
Subject: Re: Columns defined not matching pg_attibute