3

我在使用休眠的 intellij 想法执行 SQL 命令时遇到了麻烦。这是我写的代码:

import models.Employee;
import org.hibernate.ejb.HibernatePersistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import java.util.HashMap;
import java.util.List;

public class manager {
   public final static String SELECT_QUERY = new String("from Employee where id =:id");
    public static void main(String[] args){
        int id = 21577911;
        PersistenceProvider pp = new HibernatePersistence();
        EntityManagerFactory emf = pp.createEntityManagerFactory("NewPersistenceUnit", new HashMap());
        EntityManager em = emf.createEntityManager();
        List<Employee> employees = em.createQuery(SELECT_QUERY, Employee.class).setParameter("id", id).getResultList();
        System.out.println(employees);
        em.close();
    }
}

这些是错误:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:273)
    at manager.main(manager.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2036)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815)
    at org.hibernate.loader.Loader.doQuery(Loader.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2522)
    at org.hibernate.loader.Loader.doList(Loader.java:2508)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
    at org.hibernate.loader.Loader.list(Loader.java:2333)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:264)
    ... 6 more
Caused by: java.sql.SQLException: **No database selected**
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
    ... 21 more

我已经完全按照视频演示中的方式配置了 persistence.xml:http: //www.jetbrains.com/idea/features/jpa_hibernate.html

4

2 回答 2

14

显然,当您的应用程序不知道要使用什么数据库时,就会发生这种情况。要解决此问题,您应该从以下位置更改数据库驱动程序 URL:

"URL"="jdbc:mysql://localhost:3306/" 

"URL"="jdbc:mysql://localhost:3306/databasename"
于 2013-07-21T23:53:48.843 回答
0

我在 EJB 中使用休眠,而 glassfish 中发生异常

SQL Error: 1046, SQLState: 3D000
No database selected

我确实签入了 glassfish JDBC 连接池附加属性 URL 和 url 属性设置如下

jdbc:mysql://localhost:3306/

修复只是在 url 和 URL 属性中添加数据库名称

jdbc:mysql://localhost:3306/{databasename}

它起作用了,错误是

于 2020-04-13T08:53:31.123 回答