Apache Commons DbUtils 使用

news/2024/5/19 1:38:06 标签: jdbc, DbUtils

介绍

Apache Commons DbUtils
The Apache Commons DbUtils package is a set of Java utility classes for easing JDBC development.

<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.6</version>
</dependency>

简单使用


package mybatis.cn.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author yanyg
 * @Date 2020/6/22 9:01
 * @Descripetion admin
 */
public class DbUtil {
    private static String url = "jdbc:mysql://127.0.0.1:3306/nabs_base?useUnicode=true&amp;characterEncoding=utf8mb4&useSSL=false";
    private static String username = "root";
    private static String password = "root";

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
public class DBUtilsTest {

    @Test
    public  void testQuery() {
        //创建连接
        Connection conn = DbUtil.getConnection();
        //创建SQL执行工具
        QueryRunner queryRunner = new QueryRunner();
        List<IdValidLogSum> list = null;
        try {
            //执行SQL查询,并获取结果
            list = queryRunner.query(conn, "select * from person", new BeanListHandler<>(IdValidLogSum.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null) {
            //输出查询结果
            list.forEach(System.out::println);
        }
        //关闭数据库连接
        DbUtils.closeQuietly(conn);
    }

}

http://www.niftyadmin.cn/n/1364062.html

相关文章

phonegap安卓视频播放解决方案

使用phonegap开发的时候&#xff0c;视频播放很多人一开始选择用html5的Video标签作为备选方案&#xff0c;实际上这种方案在真实环境下常常是无法满足需求的&#xff0c;因为目前HTML5的Video标签只支持MP4&#xff0c;OGG以及webm格式的视频播放&#xff0c;即使对于以上视频…

利用 JDK 自带的 Document + XPath 解析 XML

利用 JDK 自带的 Document XPath 解析 XML&#xff0c;记录一下 准备工作 inventory.dtd 和 inventory.xml 文件 dtd 中的 PCDATA 的意思是被解析的字符数据&#xff08;parsed character data&#xff09;。可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。PCDA…

《APUE》读书笔记第十一章-线程

本章主要介绍了线程&#xff0c;了解如何使用多线程在单进程环境中来执行多任务。由于多个线程共享其进程空间,所以必须采用同步的机制来保护数据的一致性。 一.线程的概念 典型的Unix系统都可以看成只有一个控制线程&#xff0c;一个进程在同一时刻只能做一件事。但有了多线程…

代码开发总结

String.format 格式化 import java.time.LocalDate;/*** Author yanyg* Date 2020/7/9 9:40* Descripetion admin*/ public class TestStringFormat {public static void main(String[] args) {LocalDate localDate LocalDate.now();int year localDate.getYear();int month…

MyBatis 自定义类型处理器处理枚举类

代码 枚举类 package mybatis.cn.util;/*** Author yanyg* Date 2020/6/30 14:15* Descripetion admin*/ public enum Gender {MALE(1, "男"), FEMALE(2, "女");private Integer code;private String name;Gender(Integer code, String name) {this.code…

CSharp程序员学Android开发---2.个人总结的快捷键

最近公司组织项目组成员开发一个Android项目的Demo&#xff0c;之前没有人有Andoid方面的开发经验&#xff0c;都是开发C#的。 虽说项目要求并不是很高&#xff0c;但是对于没有这方面经验的人来说&#xff0c;第一步是最困难的。 项目历时一个多月&#xff0c;4个人开发&#…

Apache Kafka概念入门

介绍 Apache Kafka 是 一个分布式流处理平台 开发步骤 添加依赖 <dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>2.2.4.RELEASE</version> </dependency>生产者…

Jenkins 2.176.2 安装

Jenkins入门 Jenkins 是一个开源软件项目&#xff0c;是基于Java开发的一种持续集成工具&#xff0c;用于监控持续重复的工作&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件的持续集成变成可能。 Jenkins是一个开源的、可扩展的持续集成、交付、部署&#xff0…