我有一个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(); }
但是它只插入’??????’。我现在能做什么?
在db变量声明后添加此变量:
String unicode= "?useUnicode=yes&characterEncoding=UTF-8";
然后修改您的行:
con = DriverManager.getConnection(url+db,"root","");
至
con = DriverManager.getConnection(url+db+unicode,"root","");