日期:2014-05-20  浏览次数:20648 次

hibernate的中文乱码问题
我jsp页面中传值到dao中hibernate查询语句中出现中文乱码:
在search中name的值等于余明建(在用之前我获取的还是中文下面就变成了乱码)
public List search(String name ,String type)
{  
List list=new ArrayList();
try
{
session=this.getSession();
tx=session.beginTransaction();
System.out.println("在search中name的值等于"+name);
String hql="from Pet p where 1=1 ";
if(name!=null)
{
hql+="and p.petName like '"+name+"'";
}
if(type!=null)
{
hql+=" order by p."+type+" asc";
}
Query query =session.createQuery(hql);
//query.setString(0, "%name%");
list=query.list();
tx.commit();
}catch(Exception ex)
{
ex.printStackTrace();
tx.rollback();
}finally
{
this.closeSession();
}
Iterator it=list.iterator();
while(it.hasNext())
{
Pet pet=(Pet)it.next();
System.out.println("宠物名:"+pet.getPetName()+"\t"+pet.getPetOwnerName()+"\t"+pet.getPetCute()+"\t");
 
}
return list;
}
出现乱码:
Hibernate: select pet0_.pet_id as pet1_, pet0_.pet_name as pet2_2_, pet0_.pet_sex as pet3_2_, pet0_.pet_strength as pet4_2_, pet0_.pet_cute as pet5_2_, pet0_.pet_love as pet6_2_, pet0_.pet_intro as pet7_2_, pet0_.pet_owner_name as pet8_2_, pet0_.pet_owner_email as pet9_2_, pet0_.pet_password as pet10_2_, pet0_.pet_pic as pet11_2_, pet0_.pet_type as pet12_2_ from epet.dbo.PetInfo pet0_ where 1=1 and (pet0_.pet_name like '?à?÷?¨') order by pet0_.pet_strength asc


------解决方案--------------------
myeclipse.connection.profile是什么?
将<property name="myeclipse.connection.profile">pet </property> 
注释掉,把
<property name="connection.url">jdbc:sqlserver://localhost:1433 </property> 
改为
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pet</property>
试试