neemandu
neemandu

Reputation: 53

intellij idea hibernate - java.sql.SQLException: No database selected

i'm having trubble in executing an SQL command with in intellij idea with hibernate. this is the code i wrote:

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();
    }
}

and these are the errors:

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

i have configured the persistence.xml exactly as in the video demo in: http://www.jetbrains.com/idea/features/jpa_hibernate.html

Upvotes: 3

Views: 10220

Answers (2)

MHK
MHK

Reputation: 489

I am using hibernate in EJBs and exception occurred in glassfish

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

i did checked in glassfish JDBC connection pool additional propteries URL and url property was set like this

jdbc:mysql://localhost:3306/

fix is just added database name in url and URL property

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

and it worked f error was

Upvotes: 0

Williams
Williams

Reputation: 151

Apparently this occurs when your application does not know what database to use. to resolve this issue, you should change your database driver url from:

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

to

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

Upvotes: 15

Related Questions