我试图删除我的旧数据库代码,以替换为hibernate。我已经完成了大部分的地面工作,但是当我试图连接到数据库时,我似乎正在处理一些问题。
我的hibernate cfg.xml如下所示;
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"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>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">jdbc:mariadb://ip:port/schema?autoReconnect=true</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!-- List of XML mapping files -->
<mapping resource="Card.hbm.xml" />
</session-factory>
</hibernate-configuration>连接细节是从我原来的(工作的)非hibernate应用程序中直接复制粘贴作业,所以它们肯定是正确的。我的配置文件的格式有问题吗?引发的异常是
Exception in thread "main" org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:mariadb://ip:port/schema?autoReconnect=true]
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:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at com.swiped.cards.HibernateUtil.configureSessionFactory(HibernateUtil.java:15)
at com.swiped.cards.HibernateUtil.getSessionFactory(HibernateUtil.java:20)
at com.swiped.cards.Main.main(Main.java:9)我还使用sql工作台连接到这个数据库,因此我确信细节是正确的。
谢谢
发布于 2014-08-22 15:40:08
您使用了错误的连接字符串。您的配置使用MariaDB协议/方言。
要使数据库连接正常工作,只需将"mariadb“替换为"mysql”。
发布于 2014-08-22 15:33:17
您必须更改模板文件中的一些变量,它必须是这样的:我将必须更改为Uppercase的变量设置为Uppercase,这样您就更容易看到。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"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>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">jdbc:**mysql**://**HEREYOUPUTIP:HERETHEPORT/YOURSCHEMA**?autoReconnect=true</property>
<property name="hibernate.connection.username">**THEUSEROFYOURDB**</property>
<property name="hibernate.connection.password">**YOURDBPASSOWRD**</property>
<!-- List of XML mapping files -->
<mapping resource="Card.hbm.xml" />
</session-factory>
</hibernate-configuration>https://stackoverflow.com/questions/25450510
复制相似问题