将希伯来字符插入MySQL表时获取问号 [英] Getting question marks when inserting Hebrew characters into a MySQL table

查看:116
本文介绍了将希伯来字符插入MySQL表时获取问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Netbeans使用Java来构建Web应用程序,JSP使用希伯来字段处理数据库。

I'm using Netbeans building a web application using Java, JSP that handle a database with Hebrew fields.

DDL如下:

String cityTable = "CREATE TABLE IF NOT EXISTS hebrew_test.table ("
                            +"id int(11) NOT NULL AUTO_INCREMENT,"
                            +"en varchar(30) NOT NULL,"
                            +"he varchar(30) COLLATE utf8_bin NOT NULL,"
                            +"PRIMARY KEY (id)"
                            +") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;";
String insert = "INSERT INTO hebrew_test.table (en, he) VALUES ('A','a')";
String insert2 = "INSERT INTO hebrew_test.table (en, he) VALUES ('B','ב')";
String insert3 = "INSERT INTO hebrew_test.table (en, he) VALUES ('C','אבג')";


executeSQLCommand(cityTable);
executeSQLCommand(insert);
executeSQLCommand(insert2);
executeSQLCommand(insert3);

我得到的输出表格:

1   A   a
2   B   ?
3   C   ???

而不是:

1   A   a
2   B   ב
3   C   אבג

我试过希伯来语在Netbeans中出现问号,但是是不一样的问题。我在表中得到了问号。

I tried Hebrew appears as question marks in Netbeans, but that isn't the same problem. I get the question marks in the table.

我还将表定义为 UTF8_bin ,如你所见在上面的代码中。

Also I defined the table to be in UTF8_bin as you can see in the above code.

推荐答案

你需要告诉JDBC驱动程序在解码代表SQL的字符时使用UTF-8编码查询到字节。您可以通过将 useUnicode = yes characterEncoding = UTF-8 查询参数添加到JDBC连接URL来实现。

You need to tell the JDBC driver to use UTF-8 encoding while decoding the characters representing the SQL query to bytes. You can do that by adding useUnicode=yes and characterEncoding=UTF-8 query parameters to the JDBC connection URL.

jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8

否则将使用操作系统平台默认字符集。 MySQL JDBC驱动程序本身非常了解客户端(运行JDBC代码的位置)和服务器端(DB表所在的位置)中使用的编码。 DB表使用的字符集未覆盖的任何字符都将被问号替换。

It will otherwise use the operating system platform default charset. The MySQL JDBC driver is itself well aware about the encoding used in both the client side (where the JDBC code runs) and the server side (where the DB table is). Any character which is not covered by the charset used by the DB table will be replaced by a question mark.

  • Spring Encoding with CharacterEncodingFilter in web.xml

这篇关于将希伯来字符插入MySQL表时获取问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆