好的,我试着保持这个简短。 我有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 :(得分:0)
尝试创建这样的连接:
con = DriverManager.getConnection("jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8", "user", "pass");
答案 1 :(得分:0)
您如何查看已插入的数据?
您要打印的控制台可能不支持UTF-8字符集。