从Eclipse传汉字到Mysql时,查询时总是显示问号
Mysql用的字符集是utf-8,java代码如下
public static void main(String[] args) throws ClassNotFoundException, SQLException{
// TODO Auto-generated method stub
//1、加载数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2、建立数据库连接
String url = "jdbc:mysql://localhost:3306/qiuwei?CharaterEncoding=utf-8";
String user = "root";
String password = "12345";
Connection cn = DriverManager.getConnection(url, user, password);
//3、创建statement对象用来发送sql命令
Statement stmt = cn.createStatement();
String sql = "insert into accounts values('dad','adggtgf')";
int rs =stmt.executeUpdate(sql);
System.out.println(rs);
}
[解决办法]
数据库要转码