Re: Encoding Problem

Поиск
Список
Период
Сортировка
От Jerome Colombie
Тема Re: Encoding Problem
Дата
Msg-id 41E7059D.8040001@gmx.ch
обсуждение исходный текст
Ответ на Re: Encoding Problem  (Kris Jurka <books@ejurka.com>)
Список pgsql-jdbc
Hi,

I made a low-level program which prints to the console. It seems that
the jdbc driver is correct, although the Strings need some
postprocessing in Java. I want to create html output, so I don't know if
I have to change my windows settings, java settings (Locale) or just
need to reformat the strings in java code.
My test program looks like this:

*********
bh=# create table test (
  t          VARCHAR(5)
);

bh=# insert into test values ('aäöü');

bh=# select * from test;
  t
------
 aäöü
(1 row)

bh=# select getdatabaseencoding();
 getdatabaseencoding
---------------------
 LATIN1
(1 row)

bh=# SHOW client_encoding;
 client_encoding
-----------------
 LATIN1
(1 row)

bh=# select version();
                                                version
--------------------------------------------------------------------------------------------------------
 PostgreSQL 8.0.0rc2 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC)
3.3.1 (mingw special 20030804-1)
(1 row)
*********
import java.sql.*;
import java.util.Properties;

public class BaseDAO {
    protected static String dbHost = "localhost";
    protected static String dbUrl = "jdbc:postgresql://" + dbHost +
":5432/bh";
    protected static String dbUser = "postgres";
    protected static String dbPassword = "***";
    protected static String dbDriver = "org.postgresql.Driver";

    static {
        //Register driver
        try {

DriverManager.registerDriver((Driver)Class.forName(dbDriver).newInstance());
        } catch (Exception e){
            System.out.println("Error: " + e);
        }
    }

    public BaseDAO() {
        super();
        try {
                Properties props = new Properties();
                props.put("user", dbUser);
                props.put("password", dbPassword);
                Connection con = DriverManager.getConnection(dbUrl, props);
                ResultSet rs =
con.createStatement().executeQuery("select * from test");
                if (rs.next()) {
                    byte[] temp = rs.getString("t").getBytes();
              System.out.println("Database String");
              System.out.println(rs.getString("t"));
              System.out.println("Database Bytes");
              for (int i = 0; i < temp.length; i++) {
                  System.out.println(temp[i]);
              }
                }
      } catch(SQLException se) {
          System.out.println("Error: " + se);
        }
    }

    public static void main(String[] args) {
        BaseDAO dao = new BaseDAO();
        String test = "aäöü";
        System.out.println("Java String");
        System.out.println(test);
        System.out.println("Java Bytes");
        byte[] temp = test.getBytes();
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
        }
        try {
            System.out.println("ISO-8859-1");
            String t1 = new String("aäöü".getBytes(), "ISO-8859-1");
            temp = t1.getBytes();
            for (int i = 0; i < temp.length; i++) {
                System.out.println(temp[i]);
            }
        } catch (java.io.UnsupportedEncodingException e) {
            System.out.println("Encoding exception: " + e);
        }
        try {
            System.out.println("UTF-8");
            String t1 = new String("aäöü".getBytes(), "UTF-8");
            temp = t1.getBytes();
            for (int i = 0; i < temp.length; i++) {
                System.out.println(temp[i]);
            }
        } catch (java.io.UnsupportedEncodingException e) {
            System.out.println("Encoding exception: " + e);
        }

    }
}
*********
D:\temp\test>javac BaseDAO.java

D:\temp\test>java -classpath d:\projects\jar\pg74.214.jdbc3.jar;. BaseDAO
Database String
a???
Database Bytes
97
63
63
63
Java String
aõ÷³
Java Bytes
97
-28
-10
-4
ISO-8859-1
97
-28
-10
-4
UTF-8
97
63
63
*********
How can I get the correct output in html with java code? I know that
technically it hasn't got to do with jdbc but I still hope someone can
give me a solution so I don't need to change the java code. I hope I can
solve this problem by changing either the database configuration or the
java or windows locale.

Regards, Jerome



В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Oliver Jowett
Дата:
Сообщение: Re: Encoding Problem
Следующее
От: Oliver Jowett
Дата:
Сообщение: Re: Removing our datasource/pooling implementation.