很长一段时间以来,我一直在尝试连接hibernate和mysql。
数据库或intellij没有问题,因为我可以毫无问题地添加db作为数据源,并在建立连接后通过intellij sql终端创建| drop表或插入值。
但使用后 SessionFactory
出现“无法建立jdbc连接”错误。
使用 DriverManager.getConnection
工作,但它不使用hibernate,我将不得不写查询,我会避免。
我正在使用Hibernate5.3.7
编辑:修复了url中的引号,但没有帮助
edit2:所以我做了一些挖掘(调试),发现错误出现在“connections.add(connectioncreator.createconnection()”行中的“drivermanagerconnectionproviderimpl.java”行中。它的实现是basicconnectioncreator类哪个字段
final Connection conn = makeConnection( url, connectionProps );
它总是空的,并且抛出异常,但是当我调试这个时,参数是好的。
edit3:我尝试使用hibernate的早期版本,mysql服务器和sqlconnector/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)
暂无答案!
目前还没有任何答案,快来回答吧!