为什么我的程序在连接MySQL的时候输入汉字是返回到错误界面,而英文就没问题,数据库是UTF-8 ,表也是,网上的方法全试完了 小弟用的是struts1.2+Hibenate3.2+MySQL5.0
代码如下:
test.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for TestForm form</title>
</head>
<body>
<html:form action="/test" method="post">
name : <html:text property="name"/><html:errors property="name"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
控制层:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.os.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import servlet.AbstractTest;
import servlet.Test;
import com.os.form.TestForm;
import Dao.Testdao;
/**
* MyEclipse Struts
* Creation date: 08-11-2010
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request" validate="true"
* @struts.action-forward name="lose" path="/form/lose.jsp"
* @struts.action-forward name="success" path="/form/success.jsp"
*/
public class TestAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException
{
TestForm testForm = (TestForm) form;
String name = new String(testForm.getName().getBytes("iso8859-1"),"utf-8");
String password=new String(testForm.getPassword().getBytes("iso8859-1"),"utf-8");
Testdao test = new Test();
ActionForward forword = null;
try{
AbstractTest admin= test.TestLogin(name,password);
if(admin!=null)
{
forword=mapping.findForward("success");
}
else
{
forword=mapping.findForward("lose");
}
catch(Exception e)
{
}
return forword;
}
}
业务层:
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
import Dao.Testdao;
import SessionFactory.HibernateSessionFactory;
import SessionFactory.TestSessionFactory;
/**
* Test entity.
*
* @author MyEclipse Persistence Tools
*/
public class Test extends AbstractTest implements java.io.Serializable, Testdao{
// Constructors
/** default constructor */
public Test() {
}
/** minimal constructor */
public Test(Integer id) {
super(id);
}
/** full constructor */
public Test(Integer id, String name, String password) {
super(id, name, password);
}
public AbstractTest TestLogin(String name, String password) {
// TODO Auto-generated method stub
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
AbstractTest admin=null;
try{
tx=session.beginTransaction();
String hql="select a from AbstractTest as a where a.name=:name and a.password=:password";
Query query=session.createQuery(hql);
query.setString("name", name);
query.setString("password", password);
query.setMaxResults(1);
admin=(AbstractTest)query.uniqueResult();
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
{tx.rollback();}
e.printStackTrace();
}
finally
{
TestSessionFactory.closeSession();
}
return admin;
}
}
求教啊 郁闷坏了 整了两天了
[quote]Hibernate: select abstractte0_.id as id0_, abstractte0_.name as name0_, abstractte0_.password as password0_ from mytest.test abstractte0_ where abstractte0_.name=? and abstractte0_.password=? limit ? [/quote]
输出这个是对的,这是hibernate生成的查询语句。你把后面的分页去掉。你给我你的qq,咱们私聊,还能截图。
这里<%@ page language="java" pageEncoding="ISO-8859-1"%>改成
<%@ page language="java" pageEncoding="UTF-8"%>试试
然后这个转换
String name = new String(testForm.getName().getBytes("iso8859-1"),"utf-8");
String password=new String(testForm.getPassword().getBytes("iso8859-1"),"utf-8");
就不要了
就是这样不用转换了。。
String name = new String(testForm.getName().getBytes("iso8859-1"),"utf-8");
String password=new String(testForm.getPassword().getBytes("iso8859-1"),"utf-8");
直接
String name = testForm.getName();
String password=testForm.getPassword();
[quote]就是这样不用转换了。。
String name = new String(testForm.getName().getBytes("iso8859-1"),"utf-8");
String password=new String(testForm.getPassword().getBytes("iso8859-1"),"utf-8");
直接
String name = testForm.getName();
String password=testForm.getPassword();[/quote]
这样该了后,,页面的那个改了吗?[quote]这里<%@ page language="java" pageEncoding="ISO-8859-1"%>改成
<%@ page language="java" pageEncoding="UTF-8"%>试试
[/quote]
再不行,在数据库连接字符串加上编码,就值这样
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
拿肯定是你这个比较没有这样的数据,就是匹配你的用户名和密码的那条数据。
按我说的那样改了吗?页面,字符串,数据库连接编码。
左右能使用system.out.println(name),system.out.println(password)测试一些用户名和密码的值
就是再你的hibernate.cfg.xml文件里把你的数据库连接字符串改成这样的
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
就是加上了编码!
是啊。。我这个jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 这是一个形势。test是数据库名字,要改成你的。。
localhost是数据库所在主机,一般自己测试都是localhost。其他就不要改了。。端口一般都是3306
嗯,是特殊字符文件,你改成这样
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
在啊, 肯定的,解答你的问题,我不能有始没终啊。。呵呵
这里admin=(AbstractTest)query.uniqueResult();改成
[code="java"]
List list=query.list();
if(list!=null&&!list.isEmpty()){
admin=(AbstractTest)list.get(0);
}else{
admin=null;
}
[/code]