急!mysql5.0+connector/J3.0.9+tomcat5.0.14

chaozai 04-01-07

急!
配置:mysql5.0+connector/J3.0.9+tomcat5.0.14

中文数据取出来总是乱码
jdbc:mysql//localhost/ace?user=root&password=123456&useUnicode=true&characterEncoding=gb2312

用mm.git.mysql.driver2.0.8中文没问题 但是数据库如果设密码 也不行
用connector/J3.0.9 乱码 但可以设密码

求救!!!

shanmoshi
2004-01-11 14:41

用这个作为URL:
jdbc:mysql://*****/**?useUnicode=true&characterEncoding=GBK
当从输入数据的时候,有时候还需要进行转码:
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">class</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String fromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String toEncoding;<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String getFromEncoding() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> fromEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">void</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> setFromEncoding(String fromEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = fromEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String getToEncoding() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">void</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> setToEncoding(String toEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">final</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String defaultFromEncoding = </FONT><FONT style="font-family:monospaced;" COLOR="#808080">"ISO8859_1"</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">final</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String defaultToEncoding = </FONT><FONT style="font-family:monospaced;" COLOR="#808080">"GBK"</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = defaultFromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = defaultToEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter(String fromEncoding, String toEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = fromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String convert(String fromStr) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">if</FONT><FONT style="font-family:monospaced;" COLOR="#000000">(fromStr==</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">null</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> || fromStr.equals(</FONT><FONT style="font-family:monospaced;" COLOR="#808080">""</FONT><FONT style="font-family:monospaced;" COLOR="#000000">)){
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#808080">""</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
}
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">try</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">new</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String(fromStr.getBytes(fromEncoding), toEncoding);
} </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">catch</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> (UnsupportedEncodingException e) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">try</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">new</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String(fromStr.getBytes(defaultFromEncoding), defaultToEncoding);
} </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">catch</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> (UnsupportedEncodingException e1) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">null</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
}
}
}
}
</FONT>

shanmoshi
2004-01-11 14:42

用这个作为URL:
jdbc:mysql://*****/**?useUnicode=true&characterEncoding=GBK
当从输入数据的时候,有时候还需要进行转码:
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">class</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String fromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String toEncoding;<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String getFromEncoding() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> fromEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">void</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> setFromEncoding(String fromEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = fromEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String getToEncoding() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">void</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> setToEncoding(String toEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">final</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String defaultFromEncoding = </FONT><FONT style="font-family:monospaced;" COLOR="#808080">"ISO8859_1"</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">private</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">final</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String defaultToEncoding = </FONT><FONT style="font-family:monospaced;" COLOR="#808080">"GBK"</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter() {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = defaultFromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = defaultToEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> EncodingConverter(String fromEncoding, String toEncoding) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.fromEncoding = fromEncoding;
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">this</FONT><FONT style="font-family:monospaced;" COLOR="#000000">.toEncoding = toEncoding;
}<hr>

</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">public</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String convert(String fromStr) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">if</FONT><FONT style="font-family:monospaced;" COLOR="#000000">(fromStr==</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">null</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> || fromStr.equals(</FONT><FONT style="font-family:monospaced;" COLOR="#808080">""</FONT><FONT style="font-family:monospaced;" COLOR="#000000">)){
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#808080">""</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
}
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">try</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">new</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String(fromStr.getBytes(fromEncoding), toEncoding);
} </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">catch</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> (UnsupportedEncodingException e) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">try</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">new</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> String(fromStr.getBytes(defaultFromEncoding), defaultToEncoding);
} </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">catch</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> (UnsupportedEncodingException e1) {
</FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">return</FONT><FONT style="font-family:monospaced;" COLOR="#000000"> </FONT><FONT style="font-family:monospaced;" COLOR="#0000ff">null</FONT><FONT style="font-family:monospaced;" COLOR="#000000">;
}
}
}
}
</FONT>