怎样判断获取到的字符串是什么编码格式的?
如题,我获取了mysql数据库里的字符串想知道是什么编码格式的,请问用java代码怎么实现?
[解决办法]
没有这样弄过,不过mysql可以这样做,其他数据库应该也不用太关心。
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
PreparedStatement ps = conn.prepareStatement("show variables like 'character%'");
ResultSet set = ps.executeQuery();
while(set.next()){
System.out.println(set.getString(1) + " : " + set.getString(2));
}
}
//打印结果:
character_set_client : gbk
character_set_connection : gbk
character_set_database : gbk
character_set_filesystem : binary
character_set_results :
character_set_server : gbk
character_set_system : utf8
character_sets_dir : C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\