首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate cfg.xml -无法建立jdbc连接

问Hibernate cfg.xml -无法建立jdbc连接
EN

Stack Overflow用户
提问于 2014-08-22 15:25:29
回答 2查看 5.6K关注 0票数 0

我试图删除我的旧数据库代码,以替换为hibernate。我已经完成了大部分的地面工作,但是当我试图连接到数据库时,我似乎正在处理一些问题。

我的hibernate cfg.xml如下所示;

代码语言:javascript
复制
<?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应用程序中直接复制粘贴作业,所以它们肯定是正确的。我的配置文件的格式有问题吗?引发的异常是

代码语言:javascript
复制
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工作台连接到这个数据库,因此我确信细节是正确的。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-22 15:40:08

您使用了错误的连接字符串。您的配置使用MariaDB协议/方言。

要使数据库连接正常工作,只需将"mariadb“替换为"mysql”。

票数 1
EN

Stack Overflow用户

发布于 2014-08-22 15:33:17

您必须更改模板文件中的一些变量,它必须是这样的:我将必须更改为Uppercase的变量设置为Uppercase,这样您就更容易看到。

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25450510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档