开发者

Insert java utf-16 strings into postgres character fields

开发者 https://www.devze.com 2023-02-17 07:11 出处:网络
HI, I have a postgres database with latin1 charset, and a table \"user\". I need to ins开发者_StackOverflow中文版ert the name of the users using a prepared statement in java

HI, I have a postgres database with latin1 charset, and a table "user". I need to ins开发者_StackOverflow中文版ert the name of the users using a prepared statement in java

boolean success = false;  
String query = "INSERT INTO public.user (name,email) VALUES(?,?)";  
try {  
  PreparedStatement ps;  
  ps = db.prepareStatement(query);  
  ps.setString(1, user.getName());  
  ps.setString(2, user.getEmail());  
  if (ps.executeUpdate() != 0)  
    success = true;  
  ps.close();  
} catch (SQLException ex) {  
} finally {  
  return success;  
}

The problem is when user.getName() and user.getEmail() contains characters with accents like è,ò, and so on, the table store weird characters. How can save the right characters sequence from java utf-16 to postgres latin1 charset encoding?


you don't need to do anything special, the jdbc driver handles all the conversions for you. the problem, however, is that the latin1 charset cannot encode all available characters (it only supports 256 characters). so, you will lose certain characters if you attempt to put them in your table. if you are serious about storing international data, you must make your tables store some variant of unicode (utf8, utf16, etc). (you would need to fix this at the database level using some postgres specific configuratino).


jtahlborn is right about your problem.

Take a look at http://www.postgresql.org/docs/8.2/static/multibyte.html to understand multibyte encodings in postgre.

0

精彩评论

暂无评论...
验证码 换一张
取 消