package com.hkbfl.service.impl;import java.io.Serializable;
import java.util.List;import com.hkbfl.domain.Users;
import com.hkbfl.service.inter.UserServiceInter;
import com.hkbfl.util.HibernateUtil;public class UserServiceImpl extends BaseServiceImpl implements UserServiceInter { /**
 * 验证用户是否合法
 * @author 
 * @funtion 完成用户验证
 * @参数说明
 * @return 如果验证合法,返回完整的Users对象。验证失败,返回null
 */
public Users checkUsers(Users users){
String hql = "from Users where username = ? and password = ?";
String[] parameters = {users.getUsername(), users.getPassword()};
List<Users> list = HibernateUtil.executeQuery(hql, parameters);
if(list.size() == 0){
return null;
}else{
return list.get(0);
}
} public Object findById(Class clazz, Serializable id) {
// TODO Auto-generated method stub
return null;
}


}

解决方案 »

  1.   

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration> <session-factory>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.url">
    jdbc:sqlserver://localhost:1433;databaseName=30cg
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">123</property>
    <property name="connection.driver_class">
    com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="hibernate.show_sql">true</property>

    <property name="myeclipse.connection.profile">30cgDB</property>
    <mapping resource="com/hkbfl/domain/UserType.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Ordertype.hbm.xml" />
    <mapping resource="com/hkbfl/domain/PlanType.hbm.xml" />
    <mapping resource="com/hkbfl/domain/ClassType.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Apply.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Authority.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Challenge.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Curriculum.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Declaration.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Friend.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Heart.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Images.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Inspiration.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Payment.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Question.hbm.xml" />
    <mapping resource="com/hkbfl/domain/QuestionService.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Status.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Stories.hbm.xml" />
    <mapping resource="com/hkbfl/domain/StoriesRevert.hbm.xml" />
    <mapping resource="com/hkbfl/domain/StudyPlan.hbm.xml" />
    <mapping resource="com/hkbfl/domain/SuccessStories.hbm.xml" />
    <mapping
    resource="com/hkbfl/domain/SuccessStoriesRevert.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Sysdiagrams.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Target.hbm.xml" />
    <mapping resource="com/hkbfl/domain/TargetImg.hbm.xml" />
    <mapping resource="com/hkbfl/domain/TargetRevert.hbm.xml" />
    <mapping resource="com/hkbfl/domain/UserMessage.hbm.xml" />
    <mapping resource="com/hkbfl/domain/Users.hbm.xml" /> </session-factory></hibernate-configuration>
      

  2.   

    HibernateUtil包中的executeQuery方法//提供一个统一的查询方法 hql 形式 from 类  where 条件=? ..
    public static List executeQuery(String hql,String [] parameters){

    Session s=null;
    List list=null;

    try {
    s=openSession();

    Query query=s.createQuery(hql);
    System.out.println(hql);
    //先判断是否有参数要绑定
    if(parameters != null &&  parameters.length >0){
    for(int i=0;i<parameters.length;i++){
    query.setString(i, parameters[i]);
    }
    }
    list=query.list();
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
    throw new RuntimeException(e.getMessage());

    }finally{

    if(s!=null&&s.isOpen()){
    s.close();
    }

    }
    return list;
    }
      

  3.   

    楼主sql只有半句?只从from开始么?看错误信息是从30开始报错,而且是语法错误,那么说明这句sql肯定是写错了。
      

  4.   

    from Users user where user.username = ? and user.password = ?
    应该是这样吧
      

  5.   


                //先判断是否有参数要绑定
                if(parameters != null &&  parameters.length >0){    
                    for(int i=0;i<parameters.length;i++){
                        query.setString(i, parameters[i]);
                    }
                }
    楼主 以上的代码为hql语句的参数赋值,索引应该从1开始,query.setString(i, parameters[i]);改成query.setString(i+1, parameters[i]);就好了。。
      

  6.   

    hql语句没错,query.setString(i, parameters[i]);这个也没错,是不是那个session的问题
      

  7.   

    Hibernate的占位符是从0开始楼主没错啊
      

  8.   

    sql语句有没有对,放到数据库调试一下就知道了。然后在找问题。sql语句错在哪里。
      

  9.   

    你把sql语句打印出来,看下错在哪里了
      

  10.   

    同意。
    HQL应该起个别名
    改一下试试呢:
    select u from Users u where u.username = ? and u.password = ?
      

  11.   

    最TM烦HQL,蛋疼死。各种鸟问题。我用HQL就都写原生SQL。还是MYBATIS好用。 这个HQL没错,看看是不是数据库数据有问题,先用sql查一下先。
      

  12.   

    参数要改成i+1,其实看看你是否存在Users这个类
      

  13.   

    用ssh框架 sql就是这么写的
      

  14.   

    HQL占位符从0开始时没错的,又不是JDBC statement.封装的Util也没有问题,HQL不起别名也可以,所以最大可能是参数格式问题了,我觉得
      

  15.   

    实体类为啥命名为Users,为啥带s?
      

  16.   

    大哥 这是hql语句啊 都是这样写
      

  17.   

    could not execute query   通常情况为数据库连接关闭,,,  看一下你数据库是否无配置正确等等
      

  18.   

    拼Hql有问题,或者说你的参数有问题