Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用hibernate检查数据库中是否存在用户_Java_Mysql_Hibernate - Fatal编程技术网

Java 使用hibernate检查数据库中是否存在用户

Java 使用hibernate检查数据库中是否存在用户,java,mysql,hibernate,Java,Mysql,Hibernate,我是冬眠新手 我有一个UserDAO类和一个方法来确定用户是否存在于特定的表中 这是我在hb3数据库中的users表: 这是我的班级: public static void main(String[] args) { System.out.println(userExistsinDB("ABC")); } public static boolean userExistsinDB(String username) { String queryStr = "Select * fr

我是冬眠新手

我有一个
UserDAO
类和一个方法来确定用户是否存在于特定的表中

这是我在
hb3
数据库中的
users
表:

这是我的班级:

public static void main(String[] args) {
    System.out.println(userExistsinDB("ABC"));
}

public static boolean userExistsinDB(String username) {
    String queryStr = "Select * from users where username=" + username; // since username is unique
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createSQLQuery(queryStr);
    System.out.println(query.getFirstResult());

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();

    return false;
}
但是在
query.getFirstResult()
中,结果是
null
,为什么

我在用户表中有
ABC
用户名

Hibernate config file:
<?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">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hb3</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">2323</property>

        <property name="hibernate.show_sql">true</property>
        <mapping class="sajjad.htlo.User"/>
    </session-factory>
</hibernate-configuration>
Hibernate配置文件:
org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hb3
根
2323
符合事实的
结果:

Feb 20, 2015 1:19:23 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Feb 20, 2015 1:19:23 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Feb 20, 2015 1:19:23 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Feb 20, 2015 1:19:23 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hb3]
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Feb 20, 2015 1:19:23 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Feb 20, 2015 1:19:23 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Feb 20, 2015 1:19:23 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Feb 20, 2015 1:19:23 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
null
Feb 20, 2015 1:19:24 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hb3]
false
2015年2月20日凌晨1:19:23 org.hibernate.annotations.common.reflection.java.JavaReflectionManager
信息:HCANN000001:Hibernate Commons注释{4.0.4.Final}
2015年2月20日凌晨1:19:23 org.hibernate.Version日志版本
信息:hh000412:Hibernate核心{4.3.1.Final}
2015年2月20日凌晨1:19:23 org.hibernate.cfg.Environment
信息:HH000206:找不到hibernate.properties
2015年2月20日凌晨1:19:23 org.hibernate.cfg.Environment buildBytecodeProvider
信息:HH000021:字节码提供程序名称:javassist
2015年2月20日凌晨1:19:23 org.hibernate.cfg.Configuration
信息:HH000043:从资源配置:/hibernate.cfg.xml
2015年2月20日凌晨1:19:23 org.hibernate.cfg.Configuration getConfigurationInputStream
信息:hh000040:配置资源:/hibernate.cfg.xml
2015年2月20日凌晨1:19:23 org.hibernate.internal.util.xml.DTDEntityResolver解析实体
警告:HH000223:已识别过时的hibernate命名空间http://hibernate.sourceforge.net/. 使用名称空间http://www.hibernate.org/dtd/ 相反请参阅Hibernate 3.6迁移指南!
2015年2月20日凌晨1:19:23 org.hibernate.cfg.Configuration doConfigure
信息:HH000041:配置的会话工厂:null
2015年2月20日凌晨1:19:23 org.hibernate.engine.jdbc.connections.internal.driverManager连接提供MPL配置
警告:HH000402:使用Hibernate内置连接池(不用于生产!)
2015年2月20日凌晨1:19:23 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
信息:HH000401:在URL[jdbc:mysql://localhost:3306/hb3]
2015年2月20日凌晨1:19:23 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
信息:HH000046:连接属性:{user=root,password=**}
2015年2月20日凌晨1:19:23 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
信息:HH000006:自动提交模式:错误
2015年2月20日凌晨1:19:23 org.hibernate.engine.jdbc.connections.internal.driverManager连接提供MPL配置
信息:HH000115:休眠连接池大小:20(最小值=1)
2015年2月20日凌晨1:19:23 org.hibernate.dialogue.dialogue
信息:HH000400:使用方言:org.hibernate.dialogue.mysqldialogue
2015年2月20日凌晨1:19:23 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
信息:HH000399:使用默认事务策略(直接JDBC事务)
2015年2月20日凌晨1:19:23 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
信息:HH000397:使用ASTQueryTranslatorFactory
无效的
2015年2月20日凌晨1:19:24 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
信息:HH000030:清理连接池[jdbc:mysql://localhost:3306/hb3]
错误的
试试这个:

org.hibernate.Query query = session.createQuery("from users where username = :username");
query.setParameter("username", username);
query.uniqueResult();
试试这个:

org.hibernate.Query query = session.createQuery("from users where username = :username");
query.setParameter("username", username);
query.uniqueResult();

您应该使用参数化查询。它返回null,因为字符串文字应该用引号括起来。

您应该使用参数化查询。它返回null,因为字符串文字应该用引号表示法括起来