2

HI, I have a postgres database with latin1 charset, and a table "user". I need to insert 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?

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.