一尘不染

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

mysql

我有一个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();
    }

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


阅读 270

收藏
2020-05-17

共1个答案

一尘不染

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

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

然后修改您的行:

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

con = DriverManager.getConnection(url+db+unicode,"root","");
2020-05-17