我正在研究here上的示例Hibernate + MySQL应用程序。尝试运行程序时,我无法连接到数据库。 Here是堆栈跟踪。
hibernate.cfg.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookstore</property>
<property name="connection.username">root</property>
<property name="connection.password">helloWORLD12</property>
<property name="show_sql">true</property>
<mapping class="net.codejava.hibernate.Book" />
</session-factory>
</hibernate-configuration>
我要去哪里错了?我检查了数据库的用户名和密码是否正确。
答案 0 :(得分:1)
这可能是因为您使用的是旧版的MySQL驱动程序。您应该尝试使用最新版本。
要获取最新版本,可以check
我认为突然我们不应该使用更高版本,因为它会引起更多问题。我刚刚更新了最新的5.1.x版本。您可以在此处下载或将其添加到pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
答案 1 :(得分:0)
基于堆栈跟踪问题,服务器似乎无法访问。
您的配置看起来不错,它应该可以正常工作。在服务器和端口上运行测试,以检查您使用的服务器和端口是否正确。
答案 2 :(得分:0)
尝试
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">helloWORLD12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>