1

连接到 postgresql 时出现异常,

INFO: HHH000046: Connection properties: {user=postgres, password=****}
Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.sri.hibernate.main.MainClass.main(MainClass.java:16)

这是我的 hibernate.cfg.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory>
       <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
       <property name="hibernate.connection.url">"jdbc:postgresql://localhost:5432/TestDataBase</property>
       <property name="hibernate.connection.username">postgres</property>
       <property name="hibernate.connection.password">1234</property>
       <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
       <property name="show_sql">true</property>
       <property name="format_sql">true</property>
       <property name="hibernate.hbm2ddl.auto">create-drop</property>
       <mapping class="org.sri.hibernate.beans.UserDetails"/>
     </session-factory>
    </hibernate-configuration>

我的连接 URL、用户名和密码正确。我试图通过使用连接,

public static void main(String args[])
    {
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session ss=sf.openSession();
        Transaction tran=null;

        UserDetails det= new UserDetails();
        det.setDateOfBirth(new Date());
        det.setUserName("SomeName");

        try
        {
            tran=ss.beginTransaction();
            ss.save(det);
            tran.commit();
            ss.close();

        }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }

    }

我使用相同的代码连接 Oracle DB(在更改 xml 中的属性之后)它工作正常,我是否缺少特定于 postgres 的内容?请帮忙

4

0 回答 0