該死的!不得不讓我說臟話了!莫名其妙的問題。
現(xiàn)在算是比較清楚了。最近正在做一個分布式的系統(tǒng)的整合。中心數(shù)據(jù)庫用的utf8的編碼,以前的老的系統(tǒng)用的是latin1的編碼。
在latin1的編碼中插入和查詢數(shù)據(jù):
不用在連接字符上花功夫。
只要下面這個類,把中文轉(zhuǎn)換為latin1的編碼,因為默認的連接是lanti1的所以還要另外的連接字符串嗎?
1
/*
2
* Created on 2005-8-15
3
*
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
6
*/
7
package com.motel168.util;
8
9
/**
10
* @author qiya
11
*
12
* TODO To change the template for this generated type comment go to Window -
13
* Preferences - Java - Code Style - Code Templates
14
*/
15
public class Chinese {
16
public static String toChinese(String iso){
17
String gb;
18
try{
19
if(iso.equals("") || iso == null){
20
return "";
21
}
22
else{
23
iso = iso.trim();
24
gb = new String(iso.getBytes("ISO-8859-1"),"GB2312");
25
return gb;
26
}
27
}catch(Exception e){
28
System.err.print("編碼轉(zhuǎn)換錯誤:"+e.getMessage());
29
return "";
30
}
31
}
32
public static String toLatin(String iso){
33
String gb;
34
try{
35
if(iso.equals("") || iso == null){
36
return "";
37
}
38
else{
39
iso = iso.trim();
40
gb = new String(iso.getBytes("GB2312"),"ISO-8859-1");
41
return gb;
42
}
43
}catch(Exception e){
44
System.err.print("編碼轉(zhuǎn)換錯誤:"+e.getMessage());
45
return "";
46
}
47
}
48
49
}
50

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

在utf8編碼的那一段更簡單,所有的編碼設(shè)為utf8。
上次mysql中文問題提到過,就不再提了。
另外使用hibernate的時候,也會出現(xiàn)一些中文問題,這時候需要進行如下設(shè)置:
在hibernate.cfg.xml的配置文件中加入:
<property name="connection.characterEncoding">UTF-8</property>
同樣不需要在連接字符串上加入?yún)?shù)。
然后使用Filter:
在web.xml中加入如下信息:
<filter>對應(yīng)的類為:
<filter-name>filter-name</filter-name>
<filter-class>com.motel168.util.SetEncodeFilter</filter-class>
<init-param>
<param-name>defaultencoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter-name</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
package com.motel168.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetEncodeFilter implements Filter {
protected FilterConfig filterConfig = null;
protected String defaultEncoding = null;
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(selectEncoding(request));
chain.doFilter(request, response);
}
public void destroy() {
this.defaultEncoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return this.defaultEncoding;
}
}