无法建立JDBC连接[jdbc:derby:// localhost:1527 / db_thermium; create = true]

时间:2017-08-21 02:51:18

标签: java hibernate jdbc derby javadb

我第一次尝试使用JavaDB( Apache Derby )作为数据库,用于毕业项目(IT技术人员),但我在连接到pre时遇到问题创建了数据库。

我正在使用 Hibernate 作为连接框架,但我无法完成连接!前面是异常的痕迹,我试图在数据库中持续1º时间后立即得到它。

Exception in thread "main" org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:derby://localhost:1527/db_thermium;create=true]
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    at org.hibernate.internal.AbstractSharedSessionContract.getJdbcConnectionAccess(AbstractSharedSessionContract.java:424)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:102)
    at org.hibernate.internal.AbstractSharedSessionContract.<init>(AbstractSharedSessionContract.java:194)
    at org.hibernate.internal.AbstractSessionImpl.<init>(AbstractSessionImpl.java:29)
    at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:246)
    at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1193)
    at org.hibernate.internal.SessionFactoryImpl.buildEntityManager(SessionFactoryImpl.java:572)
    at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:559)
    at org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:154)
    at gerenciador.EntityManagerUtil.getEntityManager(EntityManagerUtil.java:16)
    at gerenciador.GenericDAO.salvar(GenericDAO.java:27)
    at main.Programa.inserirElemento(Programa.java:32)
    at main.Programa.iniciar(Programa.java:19)
    at main.Programa.main(Programa.java:14)

 main.Programa.main(Programa.java:14)

。这是持久性文件文本:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="teste" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
            <property name="javax.persistence.jdbc.username" value="usuario" />
            <property name="javax.persistence.jdbc.password" value="123" />
            <property name="javax.persistence.jdbc.url"    value="jdbc:derby://localhost:1527/db_thermium;create=true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
            <property name="show_sql" value="true"/>
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

EntityManager

package gerenciador;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class EntityManagerUtil {
    private static EntityManagerFactory emf = null;

    public static EntityManager getEntityManager() {
         if (emf == null){
            emf = Persistence.createEntityManagerFactory("teste");
         }
         return emf.createEntityManager();
    }



    public static EntityManagerFactory getEmf() {
        return emf;
    }



    public static void closeEntityFactory() {
        if (emf != null)
            emf.close();
    }
}

GenericDAO

package gerenciador;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

public abstract class GenericDAO<T, I extends Serializable> {

   @Inject
   private Class<T> persistedClass;

   protected GenericDAO() {
   }

   protected GenericDAO(Class<T> persistedClass) {
       this();
       this.persistedClass = persistedClass;
   }

   public T salvar(T entity) {
       EntityManager em = EntityManagerUtil.getEntityManager();
       EntityTransaction t = em.getTransaction();
       t.begin();
       em.persist(entity);
       em.flush();
       t.commit();
       EntityManagerUtil.closeEntityFactory();
       return entity;
   }

   public T atualizar(T entity) {
       EntityManager em = EntityManagerUtil.getEntityManager();
       EntityTransaction t = em.getTransaction();
       t.begin();
       em.merge(entity);
       em.flush();
       t.commit();
       EntityManagerUtil.closeEntityFactory();
       return entity;
   }

   public void remover(I id) {
       T entity = encontrar(id);
       EntityManager em = EntityManagerUtil.getEntityManager();
       EntityTransaction t = em.getTransaction();
       t.begin();
       T mergedEntity = em.merge(entity);
       em.remove(mergedEntity);
       em.flush();
       t.commit();
       EntityManagerUtil.closeEntityFactory();
   }

   public List<T> getList() {
       EntityManager em = EntityManagerUtil.getEntityManager();
       CriteriaBuilder builder = em.getCriteriaBuilder();
       CriteriaQuery<T> query = builder.createQuery(persistedClass);
       query.from(persistedClass);
       return em.createQuery(query).getResultList();
   }

   public T encontrar(I id) {
       EntityManager em = EntityManagerUtil.getEntityManager();
       return em.find(persistedClass, id);
   }
}

The ij connection test result

0 个答案:

没有答案