使用hibernate配置数据库连接时失败报错

问题描述


在配置Hibernate的数据库连接的Driver,url等参数时,连接失败。我使用的是hibernate 5.1.17的版本,mysql是8.0.11.我之前项目平时使用的DRIVER和url都是连接正常的,但是到了hibernate就不对了 

报错语句:

aused by: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near '=Asia/Shanghai'.

我平时使用的

Driver:com.mysql.cj.jdbc.Driver

url:jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=Asia/Shanghai

问题所在


反正肯定是Driver和URL错了,最后发现是URl错了(在hibernate错,不代表其他地方是错的)

 

解决方法


在hibernate.cfg.xml  将url更改为:

jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=UTC

完整的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>
        <!--连接数据库的基本参数 -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">mima</property>

        <!--配置hibernate的方言 指定这是哪个数据库-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>



        <!-- 下面是可选参数-->
        <!-- 打印sql,可以实现将sql语句打印到后台-->
        <property name="hibernate.show_sql">true</property>

        <!--格式化sql:让上面打印的sql语句更好看,不然默认是都整合为一行 -->
        <property name="hibernate.format_sql">true</property>

        <!-- 告诉hibernate你的映射文件在那里-->
        <mapping resource="com/svllen/hibernate/demo/Customer.hbm.xml"/>


    </session-factory>
</hibernate-configuration>

 你如果使用的hibernate是5.3的话,可能你连接数据库的方法有错误(我也没验证过,是在一篇博客中发现的)。以下是解决方法

更改你的调用数据库方法

注释的代码都是之前的代码

  //1.加载Hibernate的核心配置文件:hibernate.cfg.xml
        //Configuration configuration = new Configuration().configure();
       // configuration.addResource("com/svllen/hibernate/demo/Customer.hbm.xml");

        StandardServiceRegistry sr=new StandardServiceRegistryBuilder().configure().build();
        SessionFactory sessionFactory=new MetadataSources(sr).buildMetadata().buildSessionFactory();
        //2.创建一个SessionFactory对象:类似于JDBC中连接池
        //SessionFactory sessionFactory = configuration.buildSessionFactory();

        //3.通过SessionFactory获取到Session对象:类似于JDBC中的Connection
        Session session = sessionFactory.openSession();

        //4.手动开启事务
        Transaction transaction = session.beginTransaction();
        
        //5.编写代码

        Customer customer = new Customer();
        customer.setCust_name("张三");
        customer.setCust_industry("农业");
        customer.setCust_phone("12345");
        customer.setCust_level("老总");
        session.save(customer);

        //6.提交事务
        transaction.commit();

        //7.资源释放
        session.close();
        sessionFactory.close();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Hibernate 连接数据库需要进行以下步骤: 1. 添加 Hibernate 依赖库到项目中。 2. 配置 Hibernate配置文件(hibernate.cfg.xml)。 3. 定义数据表与 Java 类之间的映射关系(通过 Hibernate 的注解或者 XML 映射文件)。 4. 编写 Java 代码调用 Hibernate API 进行 CRUD 操作。 下面是一个简单的 Hibernate 连接 MySQL 数据库的示例: 1. 添加 Hibernate 依赖库到项目中: 在 Maven 项目中,可以在 pom.xml 文件中添加以下依赖: ``` <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.30.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 配置 Hibernate配置文件(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> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> <property name="hibernate.show_sql">true</property> <mapping class="com.example.User"/> </session-factory> </hibernate-configuration> ``` 3. 定义数据表与 Java 类之间的映射关系: 可以使用注解方式或者 XML 映射文件的方式进行映射。 使用注解方式的示例: ``` @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // 省略 getter 和 setter 方法 } ``` 4. 编写 Java 代码调用 Hibernate API 进行 CRUD 操作: ``` // 加载 Hibernate 配置文件 Configuration configuration = new Configuration().configure(); // 创建 SessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(); // 创建 Session Session session = sessionFactory.openSession(); // 开启事务 Transaction transaction = session.beginTransaction(); // 新增用户 User user = new User(); user.setName("张三"); user.setAge(20); session.save(user); // 提交事务 transaction.commit(); // 关闭 Session session.close(); // 关闭 SessionFactory sessionFactory.close(); ``` 以上代码实现了向数据库中插入一条数据的操作,其他的 CRUD 操作也类似。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值