eclipse - 无法从驱动程序 [null]、用户 [null] 和 URL [null] postgres 获取连接

标签 eclipse jpa-2.0 java-ee-6

早上好

我使用 Eclipse 链接和 JPA,我想测试一个小型 native 查询,但出现错误。 请你帮帮我好吗 我在这个网站和其他网站上进行了搜索,但没有找到解决方案。 谢谢。

实体类

package entites;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Minoucha
 */
@Entity
@Table(name = "protocol")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Protocol.findAll", query = "SELECT p FROM Protocol p"),
    @NamedQuery(name = "Protocol.findByProtocolid", query = "SELECT p FROM Protocol p WHERE p.protocolid = :protocolid"),
    @NamedQuery(name = "Protocol.findByProtocolname", query = "SELECT p FROM Protocol p WHERE p.protocolname = :protocolname")})
public class Protocol implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "protocolid")
    private Integer protocolid;
    @Size(max = 64)
    @Column(name = "protocolname")
    private String protocolname;

    public Protocol() {
    }

    public Protocol(Integer protocolid) {
        this.protocolid = protocolid;
    }

    public Integer getProtocolid() {
        return protocolid;
    }

    public void setProtocolid(Integer protocolid) {
        this.protocolid = protocolid;
    }

    public String getProtocolname() {
        return protocolname;
    }

    public void setProtocolname(String protocolname) {
        this.protocolname = protocolname;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (protocolid != null ? protocolid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Protocol)) {
            return false;
        }
        Protocol other = (Protocol) object;
        if ((this.protocolid == null && other.protocolid != null) || (this.protocolid != null && !this.protocolid.equals(other.protocolid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entites.Protocol[ protocolid=" + protocolid + " ]";
    }

}

主类

package main;

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Minoucha
 */
public class Main {


        /**
         * @param args
         */
        public static void main(String[] args) {
// TODO Auto-generated method stub

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestRestLast3PU");
            EntityManager em = emf.createEntityManager();
            try {
                EntityTransaction entr = em.getTransaction();
                entr.begin();
                Query query = em.createNativeQuery("SELECT protocolname FROM protocol");
                List stList = query.getResultList();
                Iterator stIterator = stList.iterator();
                while (stIterator.hasNext()) {
                    System.out.print("pname:" + stIterator.next());
                    System.out.println();
                }
                entr.commit();
            } finally {
                em.close();
            }
        }
    }

错误:

run:
avr. 16, 2013 12:34:24 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.0.Final
[EL Warning]: 2013-04-16 12:34:24.756--ServerSession(8630548)--PersistenceUnitInfo TestTP3RestPU has transactionType RESOURCE_LOCAL and therefore jtaDataSource will be ignored
[EL Info]: 2013-04-16 12:34:24.776--ServerSession(8630548)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Severe]: 2013-04-16 12:34:24.789--ServerSession(8630548)--Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:294)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:272)
    at main.Main.main(Main.java:23)
Caused by: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:488)
    ... 5 more
Java Result: 1

最佳答案

我也遇到了同样的异常。

遵循https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example处的文档如果您正在使用 JPA 的持久性单元,则缺少一个步骤。

像这样声明 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="TestPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source>
    <class>com.foo.Entity</class>
  </persistence-unit>
</persistence>

有两件事让它对我有用:

  • persistence-unit 元素的声明中添加属性 transaction-type="JTA"
  • 在数据源定义中添加 java:comp/env/

关于eclipse - 无法从驱动程序 [null]、用户 [null] 和 URL [null] postgres 获取连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16034208/

相关文章:

java - 在 Java Swing 应用程序中实现 WebSocket

Java EE 6 和替代品

python - 如何在Eclipse pyDev中创建python发行包

c++ - 从 Eclipse CDT 项目在 Linux 上编译后如何删除自动发现的路径?

java - JPA中的显式和隐式JOIN有什么区别? (表现)

java - Hibernate 4.3.6 DDL生成:索引中包含的列的大写名称

java - 使用 hibernate 更新数据库时如何避免重复记录

java - Eclipse插件: create a treeview that displays all the deprecated methods

java - 如何从 Eclipse 导出包含引用库的 jar 文件?

mysql - 使用 JPA/Spring/Hibernate 自动截断字符串列的方法?