java中中文问题

1、如果一个数据库的编码是iso8859-1的,我在java代码中使用什么方式正确读写数据库中的数据呢?
2、如果是在开发web方面的程序,又应该在中文问题上注意些什么呢?
3、其他数据库编码在java程序中的成功转换也希望了解一下。

一个转码类,我经常遇到转码,都是这个类处理的,

package db;

import java.io.UnsupportedEncodingException;

public class ChangeChar {

public ChangeChar() {
}

public static String Char(String str) {
String value = "";
String check = System.getProperty("os.name").toLowerCase();
if (str != null)
try {
if (check.indexOf("win") != -1) {
value = new String(str.getBytes("ISO-8859-1"));
} else {
value = new String(str.getBytes(), "GB2312");
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return value;
}

public String gb2iso(String qs) {
try {
if (qs == null)
return "NULL";
else
return new String(qs.getBytes("gb2312"), "iso-8859-1");
} catch (Exception e) {
}
return "NULL";
}

public String Convert(String s){

String result = null;

byte[] temp = null ;

try {
temp = s.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

try {
result =  new String(temp,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;

}

public String Convert1(String s){

String result = null;

byte[] temp = null ;

try {
temp = s.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

try {
result =  new String(temp,"iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return result;

}


} 

 

所有编码都统一用UTF-8
1.页面用UTF-8
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <META http-equiv=Content-Type content="text/html; charset=UTF-8">

2.request过来的也要用UTF-8进行编码
String domain = BaseFunc.RequestForm(request,"domain","");
domain=new String(domain.getBytes("iso-8859-1"),"UTF-8")

3.数据库要用UTF-8编码
create TABLE `t_diary_domain_zhcn` (
  `DATE` date NOT NULL,
  `DOMAIN` text,
  PRIMARY KEY  (`DATE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4.数据库连接串要用UTF-8编码
      url="jdbc:mysql://localhost/domain?useUnicode=true&amp;characterEncoding=UTF-8"

5.从数据库取出的数据也要用UTF-8编码
domain=new String(domain.getBytes("iso-8859-1"),"UTF-8");

 

 

 

1.在jsp页面加入
<%request.setCharacterEncoding("utf-8");%>
2.直接转换
String strother = "可以把is08859-1转换成gb2312!";
String strchina = new String(strother.getBytes("iso8859-1"),"gb2312");
3.tomcat 中设置
<Connector port="8009"
  enableLookups="false" redirectPort="8443" protocol="AJP/1.3" uRIEncoding="gb2312" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值