Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--
_________________________________
Pulchritudo splendor veritatis.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux