栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC——编写工具类实现Mysql数据库查询操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC——编写工具类实现Mysql数据库查询操作

步骤一:导入所需要的jar包(略)如图:

步骤二:编写properties配置文件

具体参考内容如下:

url=jdbc:mysql://localhost:3306/db_wwq
username=root
password=root
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000

步骤三:编写JdbcUtils工具类java文件

public class JdbcUtils {
    private static DataSource dataSource;
    private static ThreadLocal threadLocal;

    static {

        try {
            //加载连接池配置文件,创建DataSource
            Properties properties = new Properties();
            properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
            //初始化ThreadLocal对象
            threadLocal = new ThreadLocal<>();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取数据库连接池
    public static Connection getConnection(){
        //从threadlocal获取连接
        Connection connection = threadLocal.get();
        if (connection==null){
            try {
                connection = dataSource.getConnection();
                threadLocal.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    //释放资源
    public static void closeResource(){
        Connection connection = threadLocal.get();
        if (connection != null){
            try {
                connection.close();
                threadLocal.remove();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

步骤四:编写TestUtils测试类java文件

public class TestUtils {
    public static void main(String[] args) {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            Connection connection = JdbcUtils.getConnection();
            String sql = "select * from stu";
            preparedStatement = connection.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("sname"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        finally {
            try {
                rs.close();
                preparedStatement.close();
                JdbcUtils.closeResource();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}

步骤五:运行代码查看运行结果:

五月 13, 2022 3:10:24 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
liuYi
chenEr
zhangSan
liSi
wangWu
zhaoLiu
sunQi
zhouBa
wuJiu
zhengShi
xxx
diedie

Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/881285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号