如何使用java将阿拉伯语单词插入mysql数据库

时间:2022-09-27 16:38:05

I have a java application, want to insert arabic words to mysql database, my code looks like

我有一个java应用程序,想在mysql数据库中插入阿拉伯语单词,我的代码看起来像

Connection con = null;
    String url = "jdbc:mysql://localhost/";
    String db = "students";
    String driver = "com.mysql.jdbc.Driver";
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url+db,"root","");
        Statement st = con.createStatement();
        String name = new String(txtName.getText().getBytes(), "UTF-8");
        int val = st.executeUpdate("insert into student(name, roll) VALUES('"+name+"','"+txtRoll.getText()+"')");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

But it only insert '??????'. what can i do now?

但它只插入'??????'。我现在能做什么?

4 个解决方案

#1


19  

add this variable after db variable declaration:

在db变量声明后添加此变量:

String unicode= "?useUnicode=yes&characterEncoding=UTF-8";

and then modify your line:

然后修改你的行:

con = DriverManager.getConnection(url+db,"root","");

to

con = DriverManager.getConnection(url+db+unicode,"root","");

#2


1  

i faced the same problem but with Chinese data, and it is not only the UTF8 in the db connection is the solution you should also do this:

我遇到了同样的问题但是使用中文数据,并且不仅数据库连接中的UTF8是解决方案,您还应该这样做:

Ensure that your MySQL configuration encoding is defined correctly. Add these lines to either my.cnf "in the case if using linux" or my.ini "case you using windows"

确保正确定义了MySQL配置编码。将这些行添加到my.cnf“如果使用linux的情况下”或my.ini“case you using you”

[client] 
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8
character-set-server=utf8

#3


0  

You have to create your database as a UTF-8 table otherwise no matter what you add it will never be able to store it...

您必须将数据库创建为UTF-8表,否则无论您添加什么,它都将永远无法存储它...

second you need to encode it before you save it, I like to use unicode... one thing about this is that if you have a column that specifies 32 length, you will have to increase that for unicode as a single character takes up 6 characters,

第二,你需要在保存它之前对其进行编码,我喜欢使用unicode ...有一件事是,如果你有一个指定32长度的列,你将不得不增加unicode,因为单个字符占用6字符,

example

letter h appears as U+0068

字母h显示为U + 0068

#4


0  

@maerics I tried using PreparedStatement instead and SQL Injection attack is still able to be done, I think either am implementing the PreparedStatemnt wrong or your feed is incorrect.

@maerics我尝试使用PreparedStatement而SQL注入攻击仍然可以完成,我认为要么实现PreparedStatemnt错误,要么你的feed不正确。

#1


19  

add this variable after db variable declaration:

在db变量声明后添加此变量:

String unicode= "?useUnicode=yes&characterEncoding=UTF-8";

and then modify your line:

然后修改你的行:

con = DriverManager.getConnection(url+db,"root","");

to

con = DriverManager.getConnection(url+db+unicode,"root","");

#2


1  

i faced the same problem but with Chinese data, and it is not only the UTF8 in the db connection is the solution you should also do this:

我遇到了同样的问题但是使用中文数据,并且不仅数据库连接中的UTF8是解决方案,您还应该这样做:

Ensure that your MySQL configuration encoding is defined correctly. Add these lines to either my.cnf "in the case if using linux" or my.ini "case you using windows"

确保正确定义了MySQL配置编码。将这些行添加到my.cnf“如果使用linux的情况下”或my.ini“case you using you”

[client] 
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8
character-set-server=utf8

#3


0  

You have to create your database as a UTF-8 table otherwise no matter what you add it will never be able to store it...

您必须将数据库创建为UTF-8表,否则无论您添加什么,它都将永远无法存储它...

second you need to encode it before you save it, I like to use unicode... one thing about this is that if you have a column that specifies 32 length, you will have to increase that for unicode as a single character takes up 6 characters,

第二,你需要在保存它之前对其进行编码,我喜欢使用unicode ...有一件事是,如果你有一个指定32长度的列,你将不得不增加unicode,因为单个字符占用6字符,

example

letter h appears as U+0068

字母h显示为U + 0068

#4


0  

@maerics I tried using PreparedStatement instead and SQL Injection attack is still able to be done, I think either am implementing the PreparedStatemnt wrong or your feed is incorrect.

@maerics我尝试使用PreparedStatement而SQL注入攻击仍然可以完成,我认为要么实现PreparedStatemnt错误,要么你的feed不正确。