2013-06-20 43 views
0

好吧生病了,尽量保持这个短。 我有Java网站和MySQL数据库。一切都使用UTF-8字符集。 问题是,如果我尝试在数据库中插入立陶宛字母,所有字母都会被转换为问号,如“ąèèįįųūū”。爪哇立陶宛字符编码

Insert code: 
public static boolean addUser(UserEntity userEntity) { 
    Connection con = getDataBaseConnection(); 
    PreparedStatement statement = null; 
    try { 
     if (userEntity.isIsActive()) { 
      booleanInt = 1; 
     } else { 
      booleanInt = 0; 
     } 
     if (con != null && !con.isClosed()) { 
      statement = con.prepareStatement("INSERT INTO users (login," 
        + " firstName," 
        + " lastName," 
        + " personalId," 
        + " isActive," 
        + " password)" 
        + "VALUES(?,?,?,?,?,?)"); 
      statement.setString(1, userEntity.getLogin()); 
      statement.setString(2, userEntity.getFirstName()); 
      statement.setString(3, userEntity.getLastName()); 
      statement.setString(4, userEntity.getPersonalId()); 
      statement.setInt(5, booleanInt); 
      statement.setString(6, userEntity.getPassword()); 
      System.out.println(statement.toString()); 
      //statement.executeUpdate(); 
      return statement.execute(); 
     } 
    } catch (Exception ex) { 
     System.out.println(ex); 
    } finally { 
     closeConnection(con); 
     return false; 
    } 

如果我们打印出userEntity.getLogin,它会打印出所有包含字母的内容。让我们说它的Mažeikiai。如果我们打印出准备好的SQL语句,它将被替换。 Java也被设置为与UTF-8,GlassFish,数据库一起工作。

+0

数据库表/列是否也使用UTF8? 它看起来像一个重复:http://stackoverflow.com/questions/3828818/java-preparedstatement-utf-8-character-problem –

+0

可能重复[Java立陶宛信件](http://stackoverflow.com/questions/) 7222475/JAVA - 立陶宛语 - 字母) – Raedwald

回答

0

尝试建立连接是这样的:

con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass"); 
0

你如何查看已插入的数据?

您打印到的控制台可能不支持UTF-8字符集。