简体   繁体   中英

Java Lithuanian character encoding

Okay ill try to keep this short. I have Java web site and MySQL Database. Everything uses UTF-8 charset. Problem is that if i try to insert Lithuanian letters into database all letters like "ą č ę ė į ų ū" are converted into question marks.

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;
    }

If we print out for userEntity.getLogin it prints out everything with letters as it should be. Lets say its Mažeikiai. If we print out prepared SQL statement it will be replaced. Java is set to work with UTF-8, GlassFish, database also.

尝试像这样创建连接:

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

How are you viewing the data that you have inserted?

It is possible that the console that you are printing to is not supporting a UTF-8 character set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM