当前位置: 首页 > 知识库问答 >
问题:

尝试使用eclipse和hibernate 5连接mysql数据库

贡正诚
2023-03-14
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.moath.hibernate.HibernateMainTest.main(HibernateMainTest.java:17)
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection ["jdbc:mysql://localhost:3306/moathdb"] at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60) at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106) at org.hibernate.engine.jdbc.connections.internal.PooledConnections.(PooledConnections.java:40) at org.hibernate.engine.jdbc.connections.internal.PooledConnections.(PooledConnections.java:19) at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name = "user_details")
public class UserDetails {

@Id
@Column(name = "id")
private int userId;
@Column(name = "user_name")
private String userName;

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}
public static void main(String[] args) {

    UserDetails userDetails = new UserDetails();
    userDetails.setUserId(1);
    userDetails.setUserName("First User");

    SessionFactory sessionFactory = new 
    Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(userDetails);
    session.getTransaction().commit();
    session.close();

}

这是hibernate配置文件:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

    <!-- Database connection settings -->
    <property 
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property 
name="hibernate.connection.url">"jdbc:mysql://localhost:3306/moathdb" 
</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Disable the second-level cache -->
    <property  name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

</session-factory>

我已经检查了数据库用户名和密码,所有的东西都正确,但我仍然得到这个错误。

共有1个答案

戚奇略
2023-03-14

如果您使用的是MySQL5.x

org.hibernate.dialect.MySQL5Dialect 

作为方言。

和/或

 <property 
name="hibernate.connection.url">"jdbc:mysql://localhost:3306/moathdb" > 
property 
name="hibernate.connection.url">jdbc:mysql://localhost:3306/moathdb>
 类似资料:
  • 我试图连接到mysql数据库使用hibernate从cfg文件作为 虽然我的连接字符串、用户名、密码是正确的,并且在其他具有基本jdbc连接的项目中运行良好,但当我使用hibernate尝试它时,我在控制台上遇到了这个错误 是的,数据库服务器已启动并运行,但来自此Hibernate应用程序的连接被拒绝

  • 我正在使用Heroku,并创建了一个新的应用程序和一个Postgres空数据库设置。 我已经安装了Heroku工具带,可以登录到Heroku并成功运行以下命令: 我在这里查看activerecord选项 但是,我运气不太好。 任何Heroku/Postgres大师能帮我指明正确的方向吗?数据库为空。作为第一步,我所要做的就是成功地连接到数据库。 Ruby文件:

  • 在使用mysql连接Eclipse时出现此错误,任何人都可以提供帮助。 不建议在没有服务器身份验证的情况下建立SSL连接。 根据MySQL 5.5.45、5.6.26和5.7.6的要求,如果未设置explicit选项,默认情况下必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置显式禁用SSL,或者设置并为服

  • 所以我正在尝试使用突出显示的连接进行连接。我使用密码'abcdefghijkl'登录到SQL环境。我正在尝试连接到名为“flight_school”的数据库 我的python脚本看起来就是这样。` 导入mysql.connector mydb=mysql.connector.connect(“localhost”,“root”,“abcdefghijkl”,“flight_school”“)打印(

  • 正如标题所说,我试图通过一个单例设计模式连接到我的数据库,但它似乎不是connect,我找不到问题。 我还尝试通过main验证连通性,但控制台中没有出现“connected”消息,我也得到了以下信息:

  • 请知道,我对数据库很陌生。我能够正确安装mySQL和java连接器驱动程序。但每当我在eclipse中运行程序并尝试从我创建的数据库中检索信息时,我都会收到以下消息:“需要SSL连接,但服务器不支持”。下面是我要使用安全SSL连接运行的代码: `公共静态void main(字符串[]参数){