怎样判断获取到的字符串是什么编码格式的?

zhangfei2007cumt 2012-03-26 09:43:15
如题,我获取了mysql数据库里的字符串想知道是什么编码格式的,请问用java代码怎么实现?
...全文
498 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_zhiwei2 2013-03-13
  • 打赏
  • 举报
回复
/** * 判断字符串的编码 * * @param str * @return */ public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s = encode; return s; } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s2 = encode; return s2; } } catch (Exception exception2) { } encode = "GBK"; try { if (str.equals(new String(str.getBytes(encode), encode))) { String s3 = encode; return s3; } } catch (Exception exception3) { } return ""; }
五哥 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
没有这样弄过,不过mysql可以这样做,其他数据库应该也不用太关心。

Java code

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManage……
[/Quote]
+1
牙痴 2012-03-28
  • 打赏
  • 举报
回复
ls 正解
安特矮油 2012-03-28
  • 打赏
  • 举报
回复
没有这样弄过,不过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\
唐三角 2012-03-26
  • 打赏
  • 举报
回复
try jchardet
http://jchardet.sourceforge.net/
a java port of the source from mozilla’s automatic charset detection algorithm. It is the library for detecting the charset of webpages.
成一粒 2012-03-26
  • 打赏
  • 举报
回复
DatabaseMetaData metadata = conn.getMetaData();
好像是
成一粒 2012-03-26
  • 打赏
  • 举报
回复
这个应该是JDBC对数据库的操作。那几个方法不记得了。

50,539

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧