Java Hibernate MySql“无法建立JDBC连接”

时间:2018-11-30 22:45:56

标签: java mysql hibernate jdbc

很长一段时间我都试图连接hibernate和mysql。

数据库或Intellij没问题,因为建立连接后,我可以毫无疑问地将db添加为数据源并创建|删除表或通过Intellij sql终端将值插入到其中。 但是在使用SessionFactory之后,出现“无法建立JDBC连接”错误。

使用DriverManager.getConnection可以工作,但是它没有使用休眠模式,因此我不得不编写查询,而我会避免这种情况。

我正在使用Hibernate 5.3.7

编辑:在网址中固定引号,但没有帮助

Edit2:所以我做了一些挖掘(调试),发现在“ DriverManagerConnectionProviderImpl.java”行中出现错误,该行显示为“ connections.add(connectionCreator.createConnection()”。它的实现是BasicConnectionCreator类,该字段

final Connection conn = makeConnection( url, connectionProps );

它总是为null并引发异常,但是当我调试它时,参数很好。

Edit3:我试图使用以前版本的Hibernate,MySql Server和Sql Connector / J。我只是想知道我犯的错误有多小。

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hospital</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">pwd</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>

    <!-- DB schema will be updated if needed -->
</session-factory>

HibernateUtil.java

package com.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

  private static SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("\n\nInitial SessionFactory creation failed." + ex);             
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        // loads configuration and mappings
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // builds a session factory from the service registry
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    }
    return sessionFactory;
  }

  public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
  }

}

错误代码:

** Exception in SessionFactory **
org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:mysql://localhost:3306/hospital]
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:77)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at com.util.HibernateUtil.configureSessionFactory(HibernateUtil.java:29)
at com.util.HibernateUtil.<clinit>(HibernateUtil.java:40)
at com.Main.main(Main.java:95)
Exception in thread "main" java.lang.NullPointerException
at com.Main.main(Main.java:96)

0 个答案:

没有答案