附近的语法不正确

附近的语法不正确

本文介绍了关键字'table'附近的语法不正确,无法提取ResultSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用SQL Server创建了一个项目,其中包含以下文件:

UserDAO.java *

  public class UserDAO 
{
private static SessionFactory sessionFactory;
static
{
sessionFactory = HibernateUtility.getSessionFactory();
}

@SuppressWarnings(unchecked)
public static List< User> findAll()
{
Session session = sessionFactory.openSession();
Criteria crit = session.createCriteria(User.class);
列表<用户> userList = crit.list();
返回userList;
}
}

UserService.java

  public class UserService 
{
public static void main(String [] args)
{
List< User> ; listUsers = UserDAO.findAll();
for(User u:listUsers)
{
System.out.println(User is =+ u.getUserName());


$ b

hibernate.cfg.xml

 <?xml version =1.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate Configuration DTD // EN
http://www.hibernate.org/dtd/hibernate-configuration- 3.0.dtd>

< hibernate-configuration>
< session-factory>
< property name =hibernate.dialect> org.hibernate.dialect.SQLServer2008Dialect< / property>
< property name =hibernate.connection.driver_class> com.microsoft.sqlserver.jdbc.SQLServerDriver< / property>
< property name =hibernate.connection.url> jdbc:sqlserver:// localhost:1433; database = happy< / property>
< property name =hibernate.connection.username> lm< / property>
< property name =hibernate.connection.password> pp< / property>
< property name =hibernate.hbm2ddl.auto>建立< / property>
< property name =show_sql> true< / property>
< property name =hibernate.current_session_context_class>线程< / property>
< property name =hibernate.hbm2ddl.auto>验证< / property>
< mapping class =com.annotation.day1.entity.User/>
< / session-factory>
< / hibernate-configuration>

运行该项目后,会显示以下异常:

  Hibernate:
选择
this_.id作为id1_0_0_,
this_.password作为password2_0_0_,
this_.userName as userName3_0_0_
from
用户this_
Aug 07,2016 9:29:00 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN:SQL错误:156,SQLState:S0001
Aug 07,2016 9:29:00 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
错误:关键字'User'附近的语法不正确。在线程 主要 org.hibernate.exception.SQLGrammarException
例外:在org.hibernate.exception.internal.SQLStateConversionDelegate.convert不能提取的ResultSet
(SQLStateConversionDelegate.java:123)
。在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
。在组织。 hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
。在组织。 hibernate.loader.Loader.getResultSet(Loader.java:2065)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
在org.hibernate.loader.Loader.executeQueryStatement( Loader.java:1838)
在org.hibernate.loader.Loader.doQuery(Loader.java:909)
在org.hibernate.loader.Loader.doQueryAndInitialize NonLazyCollections(Loader.java:354)
在org.hibernate.loader.Loader.doList(Loader.java:2553)
在org.hibernate.loader.Loader.doList(Loader.java:2539)
在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
在org.hibernate.loader.Loader.list(Loader.java:2364)
在org.hibernate .loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
在org.hibernate.internal.CriteriaImpl.list (CriteriaImpl.java:380)
at com.annotation.day1.dao.UserDAO.findAll(UserDAO.java:74)
at com.annotation.day1.service.UserService.main(UserService.java :24)
导致:com.microsoft.sqlserver.jdbc.SQLServerException:关键字'User'附近的语法不正确。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)

任何帮助表示赞赏。 是,需要在查询时使用方括号[] 进行查询转义。



如果您使用注释,请使用单引号。 '',如下

  @Table(name =`user `)

另请参阅类似的问题。


I have created a project with SQL Server which the files:

UserDAO.java*

public class UserDAO
{
    private static SessionFactory sessionFactory;
    static
    {
        sessionFactory = HibernateUtility.getSessionFactory();
    }

    @SuppressWarnings("unchecked")
    public static List<User> findAll()
    {
        Session session = sessionFactory.openSession();
        Criteria crit = session.createCriteria(User.class);
        List<User> userList = crit.list();
        return userList;
    }
}

UserService.java

public class UserService
{
    public static void main(String[] args)
    {
        List<User> listUsers = UserDAO.findAll();
        for(User u : listUsers)
        {
            System.out.println("User is = " + u.getUserName());
        }
    }
}

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
       <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
       <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
       <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;database=happy</property>
       <property name="hibernate.connection.username">lm</property>
       <property name="hibernate.connection.password">pp</property>
       <property name="hibernate.hbm2ddl.auto">create</property>
       <property name="show_sql">true</property>
       <property name="hibernate.current_session_context_class">thread</property>
       <property name="hibernate.hbm2ddl.auto">validate</property>
       <mapping class="com.annotation.day1.entity.User"/>
    </session-factory>
</hibernate-configuration>

After running the project, the exception bellow displayed:

Hibernate:
    select
        this_.id as id1_0_0_,
        this_.password as password2_0_0_,
        this_.userName as userName3_0_0_
    from
        User this_
Aug 07, 2016 9:29:00 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 156, SQLState: S0001
Aug 07, 2016 9:29:00 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Incorrect syntax near the keyword 'User'.
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    at org.hibernate.loader.Loader.doQuery(Loader.java:909)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at com.annotation.day1.dao.UserDAO.findAll(UserDAO.java:74)
    at com.annotation.day1.service.UserService.main(UserService.java:24)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'User'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)

Any help is appreciated. Thanks in advance.

解决方案

USER is a Reserve Word and needs to be escaped in query using square bracket [] while querying.

If you are using annotations, escape through single quotes. '', like below

@Table(name="`user`")

Also refer here for similar issue.

这篇关于关键字'table'附近的语法不正确,无法提取ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:12