druid数据库连接池技术&Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `co

jar包和配置文件:st0b

一.步骤

在这里插入图片描述

1.导入jar包和配置文件

如下图:
在这里插入图片描述

2.项目结构

在这里插入图片描述

3.项目代码

druid.java

package druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class druid {
    public static void main(String[] args) {
        //1.导入jar包和配置文件
        //2.加载配置文件
        Properties properties = new Properties();
        InputStream inputStream = druid.class.getClassLoader().getResourceAsStream("druid.properties");
        System.out.println(inputStream);
        PreparedStatement preparedStatement1 = null;
        PreparedStatement preparedStatement2 = null;
        Connection connection = null;
        try {
            properties.load(inputStream);
            //3.获取连接池对象
            DataSource ds = DruidDataSourceFactory.createDataSource(properties);
            //4.获取连接
            connection = ds.getConnection();
            System.out.println(connection);
            connection.setAutoCommit(false);//创建事务
            String sql1 = "update account set balance = balance - ? where id = ?";
            //2.2 李四 + 500
            String sql2 = "update account set balance = balance + ? where id = ?";
            //3.获取执行sql对象
            preparedStatement1 = connection.prepareStatement(sql1);
            preparedStatement2 = connection.prepareStatement(sql2);
            //4. 设置参数
            preparedStatement1.setDouble(1, 500);
            preparedStatement1.setInt(2, 1);

            preparedStatement2.setDouble(1, 500);
            preparedStatement2.setInt(2, 2);
            //5.执行sql
            preparedStatement1.executeUpdate();
            preparedStatement2.executeUpdate();
            connection.commit();//提交事务
        } catch (Exception e) {
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            try {
                connection.close();
                preparedStatement1.close();
                preparedStatement2.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/li
username=root
password=517818li
#初始化连接数
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000

4出现错误

新版mysql已弃用com.mysql.jdbc.Driver

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
四月 19, 2020 12:31:57 下午 com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker warn
警告: Cannot resolve com.mysq.jdbc.Connection.ping method.  Will use 'SELECT 1' instead.

5.解决办法

修改配置文件

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/li
username=root
password=517818li
#初始化连接数
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000

6.成功运行截图

在这里插入图片描述

### Druid连接池中找不到MySQL驱动类解决方案 当遇到`java.lang.ClassDefNotFoundError: com.mysql.jdbc.Driver`错误时,表明应用程序未能加载指定的MySQL JDBC驱动程序。通常情况下,在较新的MySQL版本中,默认使用的驱动器名称应为`com.mysql.cj.jdbc.Driver`而非旧版中的`com.mysql.jdbc.Driver`。 对于Druid数据源配置而言,如果使用的是最新版本的MySQL,则应该确保在配置文件里设置正确的驱动类名: ```properties spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 此外,还需确认项目的依赖项已正确引入了对应的MySQL Connector/J库。可以通过Maven或Gradle来管理这些外部库。以下是通过Maven添加MySQL驱动的一个例子: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.x</version><!-- 使用具体版本号 --> </dependency> ``` 同时也要注意检查项目构建路径以及部署环境下的classpath是否包含了必要的JAR文件。有时即使本地开发环境中一切正常,但在打包发布至其他服务器上运行时可能会因为缺少某些资源而出现问题[^1]。 为了进一步排查此类问题,可以尝试以下方法之一: - 将所需的第三方库手动放置于应用服务器(如Tomcat)的全局lib目录下; - 或者调整IDEA/Eclipse等集成开发工具内的模块设置,使编译后的WAR/EAR包能够自动携带所有必需的库文件一起被打包进去[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值