程序跑起来的时候会报错误~~
调用:Hibernate.update("Z","C")
用的MySql数据库
会报以下错误:
Parameter name does not exist as a named parameter in [from Client as client where client.name=?]
数据库已经检查,存在client表,且name字段为Z的数据。
具体代码如下:
public static void update(String oldName,String newName) {
Session session=null;//Session
Transaction ts=null;//Transaction
try {
//1 update
session=HibernateUtil.getSession();//this is get Session
ts=session.beginTransaction();//this is beginTransaction
//2 select the oldName,then change the oldName to newName
String select_hql="from Client as client where client.name=?";
//Client is a Bean
Query query_select=session.createQuery(select_hql);
query_select.setString("name",oldName);
List<Client> result_list=query_select.list();//from the oldName get the right Client
for(Client cl:result_list){
System.out.println("the oldName is : "+cl.getName()+" and now is change to newName");
cl.setName(newName);//the client is in session,change to newName
}
ts.commit();
//3 select resaults
} finally {
// TODO Auto-generated catch block
if (session != null)
session.close();//close the session
}
}那个哥们儿给小弟看看,小弟Hibernate新手,刚学,不胜感激
调用:Hibernate.update("Z","C")
用的MySql数据库
会报以下错误:
Parameter name does not exist as a named parameter in [from Client as client where client.name=?]
数据库已经检查,存在client表,且name字段为Z的数据。
具体代码如下:
public static void update(String oldName,String newName) {
Session session=null;//Session
Transaction ts=null;//Transaction
try {
//1 update
session=HibernateUtil.getSession();//this is get Session
ts=session.beginTransaction();//this is beginTransaction
//2 select the oldName,then change the oldName to newName
String select_hql="from Client as client where client.name=?";
//Client is a Bean
Query query_select=session.createQuery(select_hql);
query_select.setString("name",oldName);
List<Client> result_list=query_select.list();//from the oldName get the right Client
for(Client cl:result_list){
System.out.println("the oldName is : "+cl.getName()+" and now is change to newName");
cl.setName(newName);//the client is in session,change to newName
}
ts.commit();
//3 select resaults
} finally {
// TODO Auto-generated catch block
if (session != null)
session.close();//close the session
}
}那个哥们儿给小弟看看,小弟Hibernate新手,刚学,不胜感激
解决方案 »
- 求租。
- 不能保存和打开导出的Excel文件
- 关于 Axis 源码问题 org.apache.axis.components.*
- hibernate 二级缓存问题
- 请教兄弟们:Struts中如何在一开始访问index.jsp的时候就能显示数据呢?
- ant的build.xml里面简单的一个写法看不懂
- tomcat老是出现类似问题,该怎么解决?
- 本人欲做基于P2P技术的课题??请问有什么比较好的题目吗在这方面??急!!!!????
- 高手请进来,郁闷了好些阵子也,高手呀!-----------?
- ssh2环境下hql查询
- struts表单提交中文乱码
- 很简单的问题!!!关于struts2传值时候使用动态调用的问题
改写成="select client.name from Client as client where client.name=?";
select * from Client client where client.name = ?;
注意在SQL语句实际应用中最好是列出具体查询字段而不是直接写 ‘*’。且在相应字段前加表别名前缀。
select access as data from tablename;
这句执行效果展示列名是data.而表别名是:直接 表名 接 表别名。
select t.access as data from tablename t;
//2 select the oldName,then change the oldName to newName
String select_hql="from Client as client where client.name=?";
//Client is a Bean
Query query_select=session.createQuery(select_hql);
query_select.setString("name",oldName);
很明显就是不是对应的!
是不是应该修改一下呀!
String select_hql="from Client as client where client.name=?";
query_select.setString(0,oldName);
这里应该是用序列号,来代替问好的位置,序列号默认是从0开始的,然后把oldName赋值给0对应的问号
即:的方式String select_hql="from Client as client where client.name=:name";
query_select.setString("name",oldName);
使用?的方式不能把作为参数名来赋值