mysql 中文乱码问题
public static void dbConnect()
{
//Connection conn=null;
try
{
conn=DriverManager.getConnection("jdbc:mysql://"+mysql_ip+"/"+mysql_db
+"?user="+mysql_user+"&password="+mysql_secret+");
//mylog.info("mysql connect success");
//return conn;
}
catch (
SQLException e)
{
// TODO Auto-generated catch block
//mylog.error(e);
//return null;
}
}
private static PreparedStatement ps_syslog=null;
private static String sql_syslog="insert into rvis_syslog(datetime,info) values(now(),?)";
public static void syslog(String info)
{
/*
try {
info=new String(info.getBytes(), "utf-8");
} catch (Unsupported
EncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
*/
try
{
if(ps_syslog==null||ps_syslog.isClosed())
{
ps_syslog=conn.prepareStatement(sql_syslog);
}
}
catch (SQLException e)
{
//mylog.warn(e);
dbConnect();
try
{
ps_syslog=conn.prepareStatement(sql_syslog);
}
catch (SQLException e1)
{
//mylog.error(e1);
ps_syslog=null;
}
}
try
{
ps_syslog.setString(1, info);
ps_syslog.executeUpdate();
}
catch (SQLException e)
{
//mylog.warn(e);
ps_syslog=null;
dbConnect();
}
}
java的编码是utf-8
mysql:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
为什么播入中文时都是?号
连接改成:
conn=DriverManager.getConnection("jdbc:mysql://"+mysql_ip+"/"+mysql_db
+"?user="+mysql_user+"&password="+mysql_secret+"&characterEncoding=utf-8")
也不行,这是什么问题
------解决方案--------------------安装mysql 的时候 在倒数第二步还是倒数第三部 有个选择格式的 选择utf-8
------解决方案--------------------楼上正解,或者手动修改每个字符型字段的编码
------解决方案--------------------
------解决方案--------------------数据库的是啥编码,不会是默认的那个拉丁文吧