你在weblogic中设置weblogic.codeset=gb2312就行了。
祝你好运!
祝你好运!
解决方案 »
- 数据库更新时,前台页面为什么没有跟着更新?
- hibernate的动态代理
- 谁能提供一个Struts+hibernate 项目实例噢~
- EHcache中net.sf.ehcache.CacheManager类与net.sf.ehcache.management.CacheManager有什么区别?
- 关于JMF
- soap接口请求返回 Read timed out和Connection reset
- 独立的APPLICATION程序怎样访问WEBSPHERE服务器上的EJB资源
- 只知道一个节点的名称,不知道它是哪个节点的字节点,如何获的该节点的值?
- 寻找“java数据库应用程序编程指南”的原代码的下载。
- ajax+json+jquery的小项目放在struts2里就可以,为什么放在spring里就不行?
- 请问怎样访问部署在Weblogic7中的Entity Bean的LocalHome,高分相送
- JAVA中Interface问题请教???
从你用的tomcat来看,你好像写的是jsp程序,其实jsp对中文的支持就做不是很好,尤其是对数据库的操作,所以你在写程序之前,你需要写一个javabean程序,专门用来进行中英字符转换。该javabean的程序如下:
import java.io.*;
public class g2uBean{
public String g2u(String s) {
//将字符转化为Unicode码
if(s==null || s.length()==0) {
//如果字符为空,则返回空值
return null;
}
try{
byte[] buffer = new byte[s.length()*2];
//定义了一个字节数组buffer,用来存放转化后的字节流
int j=0;
for(int i=0; i<s.length(); i++) {
//将字符转化为字节存储在字节数组中
if(s.charAt(i) >= 0x100) {
char c = s.charAt(i);
byte[] buf = (“ ”+c).getBytes();
buffer[j++] = (byte)buf[0];
buffer[j++] = (byte)buf[1];
}else{
buffer[j++] = (byte)s.charAt(i);
}
}
return new String (buffer, 0, j );
//将字节数组以字符串的形式返回
}catch (Exception erl){
byte[] b = s.getBytes();
//将字符串转化成字节数组
try{
return (new String(b,”gb2312”));
//按“GB2312编码”方式返回
}catch(UnsupportedEncodingExceptionerr){
return null;
//如果该编码方式是系统不支持的,则返回空值
}
}
}
}
然后将这个g2uBean.java文件用javac编译成g2uBean.class文件,并且将这个class文件包含于每个JSP页面中,就可以了。用法如下:
<jsp:useBean id=”g2u” scope=”page” class=”g2uBean”>
然后在编程过程中无论你用out.println(string); (string 是含中文的字符串),
还是用<%=string%>,都将语句修改成如下形式:
out.println(g2u(string));
<%=g2u(string)%>问题基本上就可以解决了,试试吧,很有用的。
import java.util.Properties;
import java.sql.*;
import java.io.*;/**
*
* This example makes a connection to a database that is using a
* multibyte codeset. You will need to specify the proper URL and codeset
* for your database. Consult your Informix installation guide for more
* information.
* <p>
* The example inserts three Unicode characters into a
* database and then performs a Select statement and reads the data back
* into a Result Set. The data is then read in via getUnicodeStream,
* converted to it's hexadecimal value, and displayed.
*
* <p><h3>Build the Example</h3>
* <ol>
* <li> Open a new command shell.
* <p><li>Set up this development shell as described in
* <a href=../../examples.html#environment>Setting up Your Environment for
* Building and Running the Examples</a>.
* <p>
* <li> Change connection parameters to correspond to your Informix Server configuration.
* If you need more help, check the section on connecting
* to a database in the programming guide, <a
* href="http://e-docs.bea.com/wls/docs70/informix4/API_jinf4.html">Using WebLogic jDriver for Informix</a>. *
* <p>
* <li>Build.xml also includes a line that sets the codeset to "<font
* face="Courier New" size=-1>cp850</font>". To change the codeset,
* search for the target named <font face="Courier New" size=-1>PreparedStatementUnicode</font>
* and change the value for -codeset.
*
* <p>
* <li>Compile this example using the following command line:
* <pre> prompt><b> ant</b></pre>
*
* <p><b>Note:</b> The <font face="Courier New" size=-1>ant</font> script builds all
* of the Informix JDBC examples.
*
* </ol>
* <p><h3>Run the Example</h3>
* <ol>
* <li>Execute the following command in your development shell:
*
* <preprompt><b> ant PreparedStatementUnicode</b></pre>
*
* </ol>
* <h3>There's More</h3>
*
* For more information, see the programming guide <a
* href="http://e-docs.bea.com/wls/docs70/informix4/API_jinf4.html">Using WebLogic jDriver for Informix</a>.
* <p>
* @author Copyright (c) 1999-2002 by BEA Systems, Inc. All rights reserved
*/
public class PreparedStatementUnicode { public static void main (String argv[])
throws SQLException { String user = "informix";
String password = "secret";
String server = "myDBHost";
String port = "1493";
String db = "myDB";
String url = "jdbc:weblogic:informix4:myDB@myDBHost:1493";
String codeset = "myCodeset";//here you set it gb2312,哈哈 try {
for (int i = 0; i < argv.length; i++)
{
if (argv[i].equals("-user")) {
i++;
user = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-password")) {
i++;
password = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-server")) {
i++;
server = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-port")) {
i++;
port = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-db")) {
i++;
db = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-url")) {
i++;
url = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-codeset")) {
i++;
codeset = (argv[i].equals("null") ? "" : argv[i]);
}
}
} catch(ArrayIndexOutOfBoundsException aiobe) {
System.err.println("\nUsage: java examples.jdbc.informix4.PreparedStatementUnicode [options] \n\n" +
"where options include:\n" +
" -user <user> User name to be passed to database.\n" +
" -password <password> User password to be passed to database.\n" +
" -server <server> DNS name of database server.\n" +
" -port <port> Port number of database server.\n" +
" -db <db> Name of database instance.\n" +
" -url <url> URL of database.\n" +
" -codeset <codeset> Name of codeset.\n");
System.exit(1);
} Driver myDriver = null;
try {
myDriver = (Driver) Class.forName("weblogic.jdbc.informix4.Driver").newInstance();
}
catch (Exception dr){
System.out.println("unable to load driver");
}
// Connect to your database. Substitute the proper database
// name, server, codeset, user, and password
Properties props = new Properties();
props.put("user", user);
props.put("password", password);
props.put("server", server); // hostname on which Informix server runs
props.put("port", port); // port on which Informix server listens
props.put("db", db); // name of database on Informix server
props.put("weblogic.codeset", codeset); // name of codeset Connection c = myDriver.connect("jdbc:weblogic:informix4", props); try {
Statement ct = c.createStatement();
ct.executeUpdate("CREATE TABLE dbtest (id smallint,tcol TEXT)");
}
catch (Exception cte){
System.out.println("database already created");
} //construct a prepared statement with one parameter
PreparedStatement ps =
c.prepareStatement("insert into dbtest values (10,?)");
// construct a UnicodeInputStream from a string and Insert it in
// the database using the prepared statement
String s = new String("\u93e1\u68b0\u897f");
weblogic.jdbc.informix4.UnicodeInputStream uis =
new weblogic.jdbc.informix4.UnicodeInputStream(s);
try {
ps.setUnicodeStream(1,uis,uis.available());
}
catch (java.io.IOException ioe) {
System.out.println("IO Exception in setUnicodeStream");
}
ps.executeUpdate();
// retrieve the data
Statement stmt = c.createStatement();
stmt.execute("select * from dbtest");
ResultSet rs = stmt.getResultSet(); InputStream uisout;
while (rs.next()) {
uisout= rs.getUnicodeStream(2);
//read in the UnicodeStream and display the bytes in hexadecimal
int i = 0;
while (true) {
try {
i = uisout.read();
} catch (IOException e) {
System.out.println("IO exception reading Unicode stream");
}
if (i == -1)
break;
System.out.println("Ox" + toHex(i) + " ");
}
}
rs.close();
stmt.close();
c.close();
} // method to convert string to hexadecimal
final static private char hex [] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' }; private static String toHex(int number) {
String s = "";
long v = number & 0xFFFFFFFF; for(int i = 0; (number > 0) || (i < 2); i++, number /= 16) {
s = hex[number % 16] + s;
}
return s;
}
}
然后无法解决
请在查询之前,doget或dopost方法中增加下面的代码:
request.setCharacterEncoding("GBK");
即可解决
//由ISO8859-1转换为gb2312。
str=new String(str.getBytes(""GB2312")," ISO8859_1");
//由gb2312转换为ISO8859-1。
检查你的字符类型,然后转换。
<jsp:useBean id=”g2u” scope=”page” class=”g2uBean”>加载过后,你在每次需要处理中文的时候,包括从数据库中取出数据,还有每个页面中对中文的取值的时候,都进行如下转换:
假设从数据库中取出数据:
sql = "SELECT * FROM details WHERE Num = " +num;
rs = bbsdb.executeQuery(sql);
String theme = null;
theme = g2u.g2uBean(rs.getString("Theme"));
g2u是你前面加载的javabean程序,
g2uBean是你的javabean的class程序名.
Theme是数据库中表格details中的一项.
这样不管你在什么地方使用这个转换过后的theme,都可以正确的显示出来。
例如在页面中写如下语句:
<FONT COLOR="#FF0080" FACE="隶书"><BIG>文章标题:<B><%=theme%></B></BIG></FONT>如果你能够明白我的意思的话,那就一切OK!
private static final String outCode = "gb2312";
private L18NString() {
}
public static String readString(String inputString){
try {
byte[] tempByte = inputString.getBytes(inCode);
inputString = new String(tempByte,outCode);
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported encoding type.");
}finally{
return inputString;
}
}
public static String writeString(String inputString){
try {
byte[] tempByte = inputString.getBytes();
inputString = new String(tempByte,inCode);
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported encoding type.");
}finally{
return inputString;
}
} public static void main(String[] args){
}
}向数据库存数据用readString(String inputString);
从数据库取数据用writeString(String inputString);
inputString为你显示和插入的数据
<%
request.setCharacterEncoding("GB2312");
%>
试一下
可解决页面中文显示问题。
new String(str.getBytes("gb2312"),"8859_1");
可解决从数据库中提取出来的中文字串的转换
还不能解决有可能就是你的驱动有问题了,重下新版试一试。
这么取就可以了!
public static String CharSet(String str) {
String temp = "";
if (str!=null) {
try {
str = str.trim();
byte[] temp_str = str.getBytes("ISO8859-1");
temp = new String(temp_str);
}
catch(Exception e) {
System.out.println("数据转换出错!错误原因:\n");
e.printStackTrace();
}
}
return temp;
}
new String(str.getBytes("GB2312"),"ISO8859-1");
从数据库往外取值的时候用
new String(str.getBytes("ISO8859_1"),"GB2312");包你没问题,当然显示数据的jsp页面也要设成gb2312