jsp里有没有较好的字符解决办法,即数据库使用latin1而jsp页面使用gbk。要求通过程序来转换而不改动数据库

来源:百度知道 编辑:UC知道 时间:2024/05/14 11:09:10
数据库是mysql4.1以上的版本。还有没有更好的办法 String dburl="jdbc:mysql://localhost:3306/DB?useUnicode=true&characterEncoding=GBK"; 在4.1以上版本没用了;tStr=new String(tStr.getBytes("ISO-8859-1"),"GBK"); 这个办法就是太繁琐所以才问有没有更好的方法

假设从数据库中读取的值保存在变量tStr中
用:tStr=new String(tStr.getBytes("这里是你读取时的字符编码"),"GBK");

例:tStr=new String(tStr.getBytes("ISO-8859-1"),"GBK");

还有一种方法,改一下数据库连接语句:
String dburl="jdbc:mysql://localhost:3306/DB?useUnicode=true&characterEncoding=GBK";
用这条语句去连接数据库,那么从数据库中读出来的数据就直接是GBK编码了,不用在JSP中转换了