继续我之前的问题 https://stackoverflow.com/questions/21060720/jsp-upload-file-with-utf-8-encoding
我发现当我这样做的时候:
String url = "jdbc:mysql://localhost:3306/crm2?useUnicode=yes&characterEncoding=UTF-8";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
connection = DriverManager.getConnection (url, MY_USERNAME, MY_PASSWORD);
PreparedStatement pst = connection.prepareStatement(sql);
.
.
.
pst.setString(1, customer.getFullName());
.
.
.
pst.executeUpdate();
我得到以下 SQL 异常:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(
name
= '?…?±?ˆ?§?†'
如您所见,我在连接字符串中提到的也使用编码 utf-8! 看起来我必须在 PST 中做一些其他事情才能确保使用 UTF-8?
谢谢
最佳答案
当使用 UTF-8 数据读取和写入数据库时,Java 源代码文件编码为 UTF-8 很重要。否则,当我们开始处理从数据库接收(或发送到)数据库的字符串时,可能会发生奇怪的事情。
关于java - JDBC 准备语句 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061388/