这样select register from Register as register where register.UserName = 'cc'
解决方案 »
- Structs框架开发
- <s:datetimepicker>标签 只显示标签,不显示下拉时间选择框
- Hibernate的使用疑问,急急急啊
- struts中session丢失
- 使用dos命令生成Axis2客户端Java类报错!
- JFreeChart里对饼图的每一部分做链接时,传汉字参数的问题
- 如何从一个打包的ejb组件jar中提取相应的home,remote,ejb类名和ejb组件中的远程调用方法和参数呢?请求大家帮忙解决啊!在线等待。。。
- 50分解决:java.lang.NullPointerException出现在jbuilderX中配置使用weblogic服务器
- 急寻电子书<<Java XML程序员参考手册>><<Java开发指南——应用XML和JSP开发电子商务>>
- 如何用Java在xml文档中插入一个节点?能否给个例子!谢谢!
- hibernate 的关联问题
- 怎么控制一个函数(里面包括很多操作)的执行超时啊?
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1515)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at cn.test.RegisterManager.query(RegisterManager.java:99)
at cn.test.RegisterManagerTest.testQuery(RegisterManagerTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operation '='
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2816)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1518)
at com.mysql.jdbc.ServerPreparedStatement.serverPrepare(ServerPreparedStatement.java:1439)
at com.mysql.jdbc.ServerPreparedStatement.<init>(ServerPreparedStatement.java:150)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1280)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1252)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:249)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:223)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:704)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
at net.sf.hibernate.loader.Loader.list(Loader.java:941)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
... 18 more
其中username的值是传进来的
String sql = "select register from Register as register where register.UserName = '"+ queryName +"'";Query query = session.createQuery(sql);应该是没有问题的
这样写,试下,而且,你要确定你的类的属性是UserName,大小写都正确。至于第而次查询出错,那时因为你已经关闭了session.请仔细检查。
虽然在第一次操作完了关闭了session
但是在第二次有重新实例化了一个session了啊,不知道为什么还是提示session已关闭
Unknown column 'cc' in 'where clause'
根本就没有cc这个列,你把UserName改成cc根本不行嘛第二个,根据你的提标,肯定是session关闭了。
如果你肯定又建立了连接,请贴出代码
祝你好运啊。
类的属性是对的,和表映射文件一致
虽然在第一次操作完了关闭了session
但是在第二次有重新实例化了一个session了啊,不知道为什么还是提示session已关闭
--------------------
上边给你都提出问题了,
可是你还非要说,你那里没有问题。
那么:回复人: zhengcg(楚楚) ( ) 信誉:99 2005-11-17 13:11:05 得分: 0
为什么不用参数的啊(最好用命名参数)?就完全没有这些什么引号匹配问题啊。还有如果你用标准的JavaBean命名register.UserName中的“UserName”肯定也是错的,应该是“userName”。
祝你好运啊。
------------------------
这是什么呀??????
Unknown column 'cc' in 'where clause'
根本就没有cc这个列,你把UserName改成cc根本不行嘛cc不是表中的一个字段,而是字段UserName 的一个字段值第二个,根据你的提标,肯定是session关闭了。
如果你肯定又建立了连接,请贴出代码
import java.util.Iterator;
import java.util.List;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;import org.apache.log4j.Logger;import cn.weixin.dao.Register;public class RegisterManager { Session session = null;
private static Logger log = Logger.getLogger(RegisterManager.class);
public RegisterManager()
{
try {
session = HibernateSessionFactory.currentSession();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setRegisterInfo(Register dao)
{
Transaction tx = null;
Register l_dao = new Register();
l_dao.setPassword(dao.getPassword());
l_dao.setUserName(dao.getUserName());
try
{
tx = session.beginTransaction();
session.save(l_dao);
tx.commit();
session.close();
}
catch (HibernateException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
} /**
*
*/
public Register[] query(String queryName) {
log.info(queryName);
Transaction tx = null;
Register l_dao = null;
Register [] l_arrReg = null;
String sql = "select register from Register as register where register.UserName = '"+ queryName +"'";
System.out.println(sql);
try
{
tx = session.beginTransaction();
Query query = session.createQuery(sql);
List list = query.list();
l_arrReg = new Register[list.size()];
list.toArray(l_arrReg);
tx.commit();
session.close();
}
catch (HibernateException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} return l_arrReg;
}}谢谢了
回复人: Quainwei(無鱗的魚) ( ) 信誉:97 2005-11-17 9:37:53 得分: 0
类的属性是对的,和表映射文件一致
虽然在第一次操作完了关闭了session
但是在第二次有重新实例化了一个session了啊,不知道为什么还是提示session已关闭
--------------------
上边给你都提出问题了,
可是你还非要说,你那里没有问题。
那么:回复人: zhengcg(楚楚) ( ) 信誉:99 2005-11-17 13:11:05 得分: 0
为什么不用参数的啊(最好用命名参数)?就完全没有这些什么引号匹配问题啊。还有如果你用标准的JavaBean命名register.UserName中的“UserName”肯定也是错的,应该是“userName”。
祝你好运啊。
------------------------
这是什么呀??????正是我觉得没有问题,或者说我找不到问题,才请教大家啊?
谢谢了
你看看你的session是如何获得的?
如果你用的是hibernateutil中的那个currentSession()的话,
不能用session.close()来关闭。
要用hibernateutil中的closeSession()来关闭;
因为hibernateUtil中把session设置到ThreadLocal中了。
如果你自己复写了HibernateUtil?贴出HibernateSessionFactory这个类
Unknown column 'cc' in 'where clause'
根本就没有cc这个列,你把UserName改成cc根本不行嘛cc不是表中的一个字段,而是字段UserName 的一个字段值========================================================
你看看错误信息::在where子句中未知的列cc
你看看hibernate生成的sql是不是把cc当成一列来,而不是你想的值
Unknown column 'cc' in 'where clause'
根本就没有cc这个列,你把UserName改成cc根本不行嘛cc不是表中的一个字段,而是字段UserName 的一个字段值========================================================
你看看错误信息::在where子句中未知的列cc
你看看hibernate生成的sql是不是把cc当成一列来,而不是你想的值========================================================但是在userName 为 111 就可以正确查询到结果我的HibernateSessionFactory是生成的import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; /** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal(); /** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration(); /** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory; /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
} return session;
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* Default constructor.
*/
private HibernateSessionFactory() {
}}
问题解决了,一定会给分的,谢了 -):
你又问一个问题应该另开一贴。
嘿嘿查看hibernate生成的hql::
在hibernate.cfg.xml 中加入
<property name="show_sql">true</property>即可如果要查看值,可以用log4j
你要记得给分啊
按username = cc 和 username = 1111查询的结果不样,传入的sql文和生成的HQL如下
select register from Register as register where register.UserName = cc
Hibernate: select register0_.id as id, register0_.userName as userName, register0_.password as password from register register0_ where (register0_.userName=cc )
net.sf.hibernate.JDBCException: Could not execute query
select register from Register as register where register.UserName = 1111
Hibernate: select register0_.id as id, register0_.userName as userName, register0_.password as password from register register0_ where (register0_.userName=1111 )
变成如下这样::
"select register from Register as register where register.UserName = "+ queryName;如果不行你把cc直接换成queryName试一下.
把上面的hql写成一句不加+号.