JDBC破坏了MySQL的特殊字符

时间:2019-02-19 13:07:03

标签: java mysql jdbc

我有一个名为deliveries的MySql 5.7.20数据库表,其中的列名为message,其类型为blob。

我希望它能够支持表情符号等特殊字符。我可以插入并查看带有表情符号的行:

enter image description here

我知道想用Java读出来。我的代码是:

public class JavaMysqlSelectExample
{

    public static void main(String[] args)
    {
        try
        {
            // create our mysql database connection
            String myDriver = "com.mysql.jdbc.Driver";
            String myUrl = "jdbc:mysql://localhost/products?useUnicode=true&characterEncoding=UTF-8";
            Class.forName(myDriver);
            Connection conn = DriverManager.getConnection(myUrl, "mark", "hht6yyt6yyt6");

            // our SQL SELECT query.
            // if you only need a few columns, specify them by name instead of using "*"
            String query = "SELECT SUBSTRING(message,1,2500) as message FROM deliveries where id = 9";

            // create the java statement
            Statement st = conn.createStatement();

            // execute the query, and get a java resultset
            ResultSet rs = st.executeQuery(query);

            // iterate through the java resultset
            while (rs.next())
            {
                String message = rs.getString("message");

                // print the results
                System.out.format("%s", message);
            }
            st.close();
        }
        catch (Exception e)
        {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }
    }
}

我的POM文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>datbase</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.jdbi/jdbi -->
        <dependency>
            <groupId>org.jdbi</groupId>
            <artifactId>jdbi</artifactId>
            <version>2.78</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
    </dependencies>


</project>

但是当我运行它时,我看到:

enter image description here

表情符号被JDBC破坏了。我可以尝试什么?

编辑

我通过实现以下方法设法读出了表情符号:

private String convertToUTF8(String str) {

    if (str == null) {
        return "";
    }

    try {
        byte[] ptext = str.getBytes("ISO-8859-1");
        return new String(ptext, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        log.error("problem converting delivery.message to UTF-8: " + e.toString());
    }

    return "";
}

现在,我可以在控制台中看到表情符号。现在还有另一个问题:这适用于我的本地mysql数据库,但不适用于AWS上的数据库。我跑了一些查询:

show variables like 'collation%';

在本地数据库上,它返回:

enter image description here

在AWS上的实例上,它返回:

enter image description here

所以区别是collation_server。而且我不知道该如何更改。我尝试跑步:

ALTER DATABASE products CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

但是collation_server的值仍然是utf8mb4_unicode_ci .....

0 个答案:

没有答案