anylin2005
2005-08-11 12:29

我再发一遍,刚才的代码没写上去
我采用了你说的方法为什么从数据库读出让他显示有问题呢??下面是我写的程序


<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>
<%request.setCharacterEncoding("UTF-8");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>
jsp2
</title>
</head>
<body bgcolor="ffffff">
<h1>
<%
String title = request.getParameter("title");
out.println(title);
%>
<%
 Connection con=null;
 try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/xinwen?useUnicode=true&amp;characterEncoding=UTF-8","root","root");
 Statement sm = con.createStatement();
 ResultSet rs =sm.executeQuery("select * from news where id=153");
if(rs.next()){
	title = rs.getString("title");
	  out.println(title);
 }
 }
 catch(SQLException ex){

 }
 catch(ClassNotFoundException ex1){
  
 }
%>
</h1>
</body>


操作系统是WIN2000SERVER版的,开发工具JBUILDER,服务器TOMCAT请指正 

yaya963
2005-08-15 09:46

// 在此输入java代码
<p>

suibian
2005-08-19 16:34
你必须设置你的mysql.ini,将其编码设置为utf-8才行。

但是这是一个很严重的问题,有可能你从前的数据无法正确读取了。

除非完全是新建一个系统,包括数据库、操作系统、应用程序全部采用统一的新的编码,否则不要轻信什么“终极的”解决办法。所谓的“终极的”解决办法都是无知者无畏的无稽之谈。

nuke7001
2005-09-19 23:13
采用了UTF-8,把中文保存到了SQLSERVER2000,用的CMP,主键用中文,在从数据库里读出来的时侯,报错,说是在数据库中找不到该项。???

liuxing2005
2005-11-05 12:26
今天我用此方法写了一个测试程序,却出现了问题,输出的乱码。

现把相关的程序代码贴在下面,还请大家帮忙看看,先谢了!

(1)first.html

〈html〉

〈body〉

大家好

〈a href="test.jsp?mychars=中文字符""\〉中文字符处理"〈/a〉

〈/body〉

〈/html〉

(2)test.jsp

〈%@page contentType="text/html;charset=utf-8"%〉

〈html〉

〈head〉

〈/head〉

〈body〉

〈%

String mchars=request.getParameter("mychars");

out.println(mchars);

%〉

〈/body〉

〈/html〉

(3)CharFilter.java ----过滤器,与楼主写的一样

package com.mypackage.util;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class CharFilter extends HttpServlet implements Filter

{

private FilterConfig filterConfig;

public void init(FilterConfig filterConfig) throws ServletException

{

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)

{

try

{

request.setCharacterEncoding("UTF-8");//设置以哪种字符过滤...

filterChain.doFilter(request,response);

}

catch(ServletException sx)

{

filterConfig.getServletContext().log(sx.getMessage());

}

catch(IOException iox)

{

filterConfig.getServletContext().log(iox.getMessage());

}

}

}

(4)web.xml

〈?xml version="1.0" encoding="UTF-8"?〉

〈!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"〉

〈web-app〉

〈display-name〉WebModule1〈/display-name〉

〈filter〉

〈filter-name〉CharFilter〈/filter-name〉

〈filter-class〉com.mypackage.util.CharFilter〈/filter-class〉

〈/filter〉

〈filter-mapping〉

〈filter-name〉CharFilter〈/filter-name〉

〈url-pattern〉/*〈/url-pattern〉

〈/filter-mapping〉

〈/web-app〉

就这些了。

以前我解决中文的问题都是通过两种方式来完成

(1)对于通过表单传输的数据,即form中text.value等的数据,是在servlet中通过设置

request.setCharacterEncoding("gb2312");来实现;

(2)对于通过重定向传输的数据,即如〈a href="test.jsp?a="传输的数据"〉传输的数据,则是用一个自己写的程序来转换,程序代码如下:

----ChineseBean.java-----

package com.science.util;

import java.io.*;

public class ChineseBean

{

public String trans(String chi)

{

String result=null;

byte temp[];

try

{

temp=chi.getBytes("iso-8859-1");

result=new String(temp);

}

catch(UnsupportedEncodingException e)

{

System.out.println(e.toString());

}

return result;

}

}

然后在jsp文件的开头写上

〈%@page contentType="text/html;charset=gb2312"%〉

就ok了。

本想用这里的方法试试,可出现了问题,还请大家帮忙看看啊!!

不过我用的JCreator编译java程序,不知道是不是这里出现了问题。

banq
2005-11-06 18:12
是这样编译环境有问题。

在编辑和编译两个环境都必须是UTF-8,也就是所有环节都必须是UTF-8,当然所有环节是gb2312也可以,但是就对操作系统要求必须是gb2312了

JunWi
2005-11-09 13:38
> 即省去各种配置步骤,又适用于所有环境(还不敢肯定,但至

> 俦救嗣慌龅焦侍猓?

嗯,我发现也不错!

yyand23
2005-12-22 11:56
请问板桥利里人:

这种方式能否解决url中的问号后的中文问题. 现在我在文号后都对中文做了encode.

非常感谢!

wanglpqpq
2006-01-09 14:24
大家好,大家知道如何处理在request(url)请求中的汉字问题如何解决吗?我们现在也是采用的utf――8解决的的乱码问题,可是当url中的参数包括汉字时候就还是乱码。

大家有什么好的解决方法吗?

feynixs
2006-03-14 17:28
所有的设置都设置了,编译,编辑,数据库链接都统一用的utf8,但还是出现乱码,实在搞不懂了,求教各位,3ks!

2006-03-14 16:26:19,703 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Opened new session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])] for Hibernate transaction
2006-03-14 16:26:19,703 DEBUG [org.hibernate.jdbc.JDBCContext] - opening user JDBC connection, application must close it
2006-03-14 16:26:19,703 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC connection to [jdbc:mysql://localhost:3306/aiweb?useUnicode=true&characterEncoding=UTF-8]
2006-03-14 16:26:19,734 DEBUG [org.hibernate.transaction.JDBCTransaction] - begin
2006-03-14 16:26:19,734 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2006-03-14 16:26:19,734 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC connection to [jdbc:mysql://localhost:3306/aiweb?useUnicode=true&characterEncoding=UTF-8]
2006-03-14 16:26:19,781 DEBUG [org.hibernate.transaction.JDBCTransaction] - current autocommit status: true
2006-03-14 16:26:19,781 DEBUG [org.hibernate.transaction.JDBCTransaction] - disabling autocommit
2006-03-14 16:26:19,781 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction begin
2006-03-14 16:26:19,781 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Exposing Hibernate transaction as JDBC transaction [com.mysql.jdbc.Connection@1b06041]
2006-03-14 16:26:19,781 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@7f5580] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1f82982] to thread [main]
2006-03-14 16:26:19,781 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Bound value [org.springframework.orm.hibernate3.SessionHolder@84cc09] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] to thread [main]
2006-03-14 16:26:19,781 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Initializing transaction synchronization
2006-03-14 16:26:19,781 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@84cc09] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] bound to thread [main]
2006-03-14 16:26:19,781 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@84cc09] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] bound to thread [main]
2006-03-14 16:26:19,921 DEBUG [org.hibernate.engine.VersionValue] - version unsaved-value strategy UNDEFINED
2006-03-14 16:26:19,921 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - transient instance of: org.feynixs.aiweb.model.User
2006-03-14 16:26:19,921 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2006-03-14 16:26:19,921 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - generated identifier: cr8@gmail.com, using strategy: org.hibernate.id.Assigned
2006-03-14 16:26:19,921 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:19,937 DEBUG [org.hibernate.engine.Versioning] - Seeding: 0
2006-03-14 16:26:19,953 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@84cc09] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] bound to thread [main]
2006-03-14 16:26:19,953 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] - Invoking commit for transaction on org.feynixs.aiweb.service.UserManager.saveUser
2006-03-14 16:26:19,953 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering beforeCommit synchronization
2006-03-14 16:26:19,953 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering beforeCompletion synchronization
2006-03-14 16:26:19,953 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Initiating transaction commit
2006-03-14 16:26:19,953 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Committing Hibernate transaction on session [SessionImpl(PersistenceContext[entityKeys=[EntityKey[org.feynixs.aiweb.model.Usercr8@gmail.com]],collectionKeys=[]];ActionQueue[insertions=[EntityInsertAction[org.feynixs.aiweb.model.Usercr8@gmail.com]] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])]
2006-03-14 16:26:19,953 DEBUG [org.hibernate.transaction.JDBCTransaction] - commit
2006-03-14 16:26:19,953 DEBUG [org.hibernate.impl.SessionImpl] - automatically flushing session
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - flushing session
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - processing flush-time cascades
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - dirty checking collections
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushing entities and processing referenced collections
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Processing unreferenced collections
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Scheduling collection removes/(re)creates/updates
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
2006-03-14 16:26:19,953 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2006-03-14 16:26:19,968 DEBUG [org.hibernate.pretty.Printer] - listing entities:
2006-03-14 16:26:19,968 DEBUG [org.hibernate.pretty.Printer] - org.feynixs.aiweb.model.User{password=test, country=中国, applicationId=null, province=广东, role=user, email=cr8@gmail.com, city=广州, firstName=刘, lastName=荣, version=0}
2006-03-14 16:26:19,968 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - executing flush
2006-03-14 16:26:19,968 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Inserting entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:19,968 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Version: 0
2006-03-14 16:26:19,968 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2006-03-14 16:26:19,968 DEBUG [org.hibernate.SQL] - insert into user (version, first_name, last_name, city, province, country, password, applicaiton_id, role, email) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2006-03-14 16:26:19,968 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2006-03-14 16:26:20,000 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Dehydrating entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.IntegerType] - binding '0' to parameter: 1
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding '刘' to parameter: 2
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding '荣' to parameter: 3
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding '广州' to parameter: 4
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding '广东' to parameter: 5
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding '中国' to parameter: 6
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding 'test' to parameter: 7
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.LongType] - binding null to parameter: 8
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding 'user' to parameter: 9
2006-03-14 16:26:20,000 DEBUG [org.hibernate.type.StringType] - binding 'cr8@gmail.com' to parameter: 10
2006-03-14 16:26:20,000 DEBUG [org.hibernate.jdbc.AbstractBatcher] - Adding to batch
2006-03-14 16:26:20,000 DEBUG [org.hibernate.jdbc.AbstractBatcher] - Executing batch size: 1
2006-03-14 16:26:20,015 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2006-03-14 16:26:20,015 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2006-03-14 16:26:20,015 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - post flush
2006-03-14 16:26:20,015 DEBUG [org.hibernate.jdbc.JDBCContext] - before transaction completion
2006-03-14 16:26:20,015 DEBUG [org.hibernate.impl.SessionImpl] - before transaction completion
2006-03-14 16:26:20,093 DEBUG [org.hibernate.transaction.JDBCTransaction] - re-enabling autocommit
2006-03-14 16:26:20,093 DEBUG [org.hibernate.transaction.JDBCTransaction] - committed JDBC Connection
2006-03-14 16:26:20,093 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction completion
2006-03-14 16:26:20,093 DEBUG [org.hibernate.jdbc.ConnectionManager] - aggressively releasing JDBC connection
2006-03-14 16:26:20,093 DEBUG [org.hibernate.jdbc.ConnectionManager] - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2006-03-14 16:26:20,093 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion
2006-03-14 16:26:20,093 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering afterCompletion synchronization
2006-03-14 16:26:20,093 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Clearing transaction synchronization
2006-03-14 16:26:20,109 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.orm.hibernate3.SessionHolder@84cc09] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] from thread [main]
2006-03-14 16:26:20,109 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@7f5580] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1f82982] from thread [main]
2006-03-14 16:26:20,109 DEBUG [org.hibernate.jdbc.JDBCContext] - opening user JDBC connection, application must close it
2006-03-14 16:26:20,109 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC connection to [jdbc:mysql://localhost:3306/aiweb?useUnicode=true&characterEncoding=UTF-8]
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Closing Hibernate session [SessionImpl(PersistenceContext[entityKeys=[EntityKey[org.feynixs.aiweb.model.Usercr8@gmail.com]],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])] after transaction
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate session
2006-03-14 16:26:20,125 DEBUG [org.hibernate.impl.SessionImpl] - closing session
2006-03-14 16:26:20,125 DEBUG [org.hibernate.jdbc.ConnectionManager] - connection already null in cleanup : no action
2006-03-14 16:26:20,125 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] - Getting transaction for org.feynixs.aiweb.service.UserManager.getUser
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@15575e0]
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Creating new transaction
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate session
2006-03-14 16:26:20,125 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 4678962299392000
2006-03-14 16:26:20,125 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Opened new session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])] for Hibernate transaction
2006-03-14 16:26:20,125 DEBUG [org.hibernate.jdbc.JDBCContext] - opening user JDBC connection, application must close it
2006-03-14 16:26:20,125 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC connection to [jdbc:mysql://localhost:3306/aiweb?useUnicode=true&characterEncoding=UTF-8]
2006-03-14 16:26:20,125 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Setting JDBC connection [com.mysql.jdbc.Connection@81b1fb] read-only
2006-03-14 16:26:20,125 DEBUG [org.hibernate.impl.SessionImpl] - setting flush mode to: NEVER
2006-03-14 16:26:20,125 DEBUG [org.hibernate.transaction.JDBCTransaction] - begin
2006-03-14 16:26:20,125 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2006-03-14 16:26:20,125 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC connection to [jdbc:mysql://localhost:3306/aiweb?useUnicode=true&characterEncoding=UTF-8]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.transaction.JDBCTransaction] - current autocommit status: true
2006-03-14 16:26:20,156 DEBUG [org.hibernate.transaction.JDBCTransaction] - disabling autocommit
2006-03-14 16:26:20,156 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction begin
2006-03-14 16:26:20,156 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Exposing Hibernate transaction as JDBC transaction [com.mysql.jdbc.Connection@81b1fb]
2006-03-14 16:26:20,156 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@1e1dadb] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1f82982] to thread [main]
2006-03-14 16:26:20,156 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Bound value [org.springframework.orm.hibernate3.SessionHolder@e79839] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] to thread [main]
2006-03-14 16:26:20,156 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Initializing transaction synchronization
2006-03-14 16:26:20,156 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@e79839] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] bound to thread [main]
2006-03-14 16:26:20,156 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@e79839] for key [org.hibernate.impl.SessionFactoryImpl@1c6572b] bound to thread [main]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - loading entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - attempting to resolve: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - object not resolved in any cache: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Fetching entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.loader.Loader] - loading entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,156 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2006-03-14 16:26:20,156 DEBUG [org.hibernate.SQL] - select user0_.email as email0_0_, user0_.version as version0_0_, user0_.first_name as first3_0_0_, user0_.last_name as last4_0_0_, user0_.city as city0_0_, user0_.province as province0_0_, user0_.country as country0_0_, user0_.password as password0_0_, user0_.applicaiton_id as applicai9_0_0_, user0_.role as role0_0_ from user user0_ where user0_.email=?
2006-03-14 16:26:20,156 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2006-03-14 16:26:20,203 DEBUG [org.hibernate.type.StringType] - binding 'cr8@gmail.com' to parameter: 1
2006-03-14 16:26:20,218 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
2006-03-14 16:26:20,218 DEBUG [org.hibernate.loader.Loader] - processing result set
2006-03-14 16:26:20,218 DEBUG [org.hibernate.loader.Loader] - result set row: 0
2006-03-14 16:26:20,218 DEBUG [org.hibernate.loader.Loader] - result row: EntityKey[org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,218 DEBUG [org.hibernate.loader.Loader] - Initializing object from ResultSet: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,218 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Hydrating entity: [org.feynixs.aiweb.model.Usercr8@gmail.com]
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.IntegerType] - returning '0' as column: version0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning '?' as column: first3_0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning '?' as column: last4_0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning '??' as column: city0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning '??' as column: province0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning '??' as column: country0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning 'test' as column: password0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.LongType] - returning null as column: applicai9_0_0_
2006-03-14 16:26:20,218 DEBUG [org.hibernate.type.StringType] - returning 'user' as column: role0_0_

lyojbuilder
2006-03-19 14:21
用了 UTF-8 才会是乱码吧 ,可以在tomcat上试试 ,只要你把

<%@page contentType="text/html;charset=GBK"%> 
改成
<%@page contentType="text/html;charset=UTF-8"%> 
,里面的中文立刻变成乱码! 大家试过吗?

banq
2006-03-23 10:23
网友在本文基础上总结的更全面文章:

http://blog.donews.com/ralix/archive/2006/03/20/777696.aspx

用UTF-8完全解决JSP+MYSQL多国语言文字编码问题

此问题这是本人经过数天努力,查遍网上所有此类文章最后自己解决的。转载请务必注明出处:

http://blog.donews.com/ralix/

首先说明几点:

在文件中任何时候出现utf-8、UTF-8、utf8、UTF8其作用都是一样的。tomcat、resin、mysql可没有我们这么笨。网上许多文章还煞有介事的特别指出其一个个的区别,纯属无稽之谈。

我的软件环境:

中文WINDOWS XP (linux可作相应修改)

Apache Tomcat 5.5.6 Server

phpMyAdmin 2.6.4-pl4

MySQL 5.0.19-nt

按下面步骤修改:

一、在JSP静态文件中正确显示UTF-8:

1、JSP文件用支持UTF-8的文字编辑器编辑,如EditPlus、UltraEdit,并把原来ascii转换成utf-8(菜单里有)。

2、JSP文件头改成:

<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*"%>

有无这句无所谓(但在.html文件中是必要的):

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

</head>

二、FORM的正确提交:

1、getParameter之前加入:

request.setCharacterEncoding("utf-8");

这一步也可以用Filter的办法,参见其它文章。

2、request时,只需要写成最简单的形式:

String name1= new String(request.getParameter("username"));

String pwd1= new String(request.getParameter("password"));

三、连接mysql:

1、mysql配置文件:

修改mysql在windows\my.ini里default-character-set=utf-8

2、mysql里数据库和表也都设为utf8_unicode_ci

3、数据库连结:jdbc:mysql://localhost/mydb?useUnicode=true&characterEncoding=utf-8

注意,关键就在于此:此句中间是'&'不是'&'在www.Jdon.com的banq和许多牛人的文章中都写成了'&'。这可能是发文章时html自动转化的。但这的确给我们带来了很大的麻烦。会产生如下错误或问号:

java.sql.SQLException: Syntax error or access violation, message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''asdf' at line 1"

至此,在页面上显示中文、从数据库读取并显示、插入、修改中文都正常了。

并且由于是utf-8,您可以试一下,在form里同时输入中文、英文、日文、韩文,照样正常显示。

PS:

虽然没有完全测试过,但在我的使用中操作系统、JDK、TOMCAT或resin的编码对此问题没有造成影响。

我测试时resin初始化显示的JDK就是GBK、zh,但没有问题。

另外,.java文件是utf-8还是ascii编码都没有关系,生成的.class一样能用。

经楼下混沌朋友的提醒,我需要补充一下:

1、数据库连结时,在.jsp和.java文件中应该用&号,而XML文件中需要用& 这也是banq的本意。只是我们太菜没能直接领会。

2、在我的测试中,connector-J已经是3.0以上但如果不加useUnicode=true&characterEncoding=utf-8访问数据库还是会有问题。所以建议还是加上。

PS2:

对用GET提交的中文问题,修改tomcat/conf/server.xml

<Connector acceptCount='100' connectionTimeout='20000' .../>最后加入:URIEncoding='utf-8'

附注:

使用MySQL官方推出的Administrator可以方便地设置mysql的缺省字符集。

使用4.1以上版MySQL和3.0以上版connector-J可以不用写?useUnicode=true&characterEncoding=utf-8。

useUnicode和charactorEncoding这两个参数的说明在connector-J的文档中有。

&和&的使用场合不同,了解了xml就好了。

jacal
2006-03-27 21:43
我一般都是用gbk的,不管在什么平台下都没有问题,我解决的方法是编写了一个转换类

package filter;

import java.net.SocketException;

import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class EncodeFilter

implements Filter

{

private String encode;

private FilterConfig config;

public EncodeFilter()

{

encode = "gb2312";

config = null;

}

public void init(FilterConfig config)

{

this.config = config;

encode = config.getInitParameter("encode");

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

{

try

{

request.setCharacterEncoding(encode);

response.setContentType("text/html;charset=" + encode);

chain.doFilter(request, response);

}

catch(Exception e)

{

if(!(e instanceof SocketException))

{

if(request instanceof HttpServletRequest)

Logger.getLogger(getClass()).error("error request uri:" + ((HttpServletRequest)request).getRequestURI());

Logger.getLogger(getClass()).error(e);

}

}

}

public void destroy()

{

config = null;

encode = null;

}

}

而在web.xml中添加以下配置,这样不管在什么环境下中文乱码的问题就都没问题了:

<filter>

<filter-name>EncodeFilter</filter-name>

<display-name>EncodeFilter</display-name>

<filter-class>filter.EncodeFilter</filter-class>

<init-param>

<param-name>encode</param-name>

<param-value>gb2312</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>EncodeFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

siyan938
2006-04-17 11:39
我的系统是繁体的,程序用的都是UTF-8.页面也是。那我的数据库是SQL SERVER的,我怎么设置数据库格式?设成chinese_PRC_CI_AS存取都出错,设成Chinese_Taiwan_Stroke_CI_AS 在碰到一些生僻字也会出错,如[等,请问这样我的那个环节出了问题,还是SQL 不支持UTF-8?

sgysgy
2007-03-29 17:04
其它没发现问题 但是用url?aaa=中文时有的字符还是乱码

郁闷

猜你喜欢