Apache DbUtils详解

2024-01-18 17:18
文章标签 详解 apache dbutils

本文主要是介绍Apache DbUtils详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1,连接类ConnectDb

import java.sql.SQLException;
import java.sql.Connection; public class ConnectDb {private static String driveClassName = "com.mysql.jdbc.Driver";private static String url = "jdbc:mysql://192.168.1.161:3306/test?useUnicode=true&characterEncoding=utf8"; private static String user = "root";private static String password = "e-playnow";public static Connection Connect(){Connection conn = null;//load drivertry {Class.forName(driveClassName);} catch (ClassNotFoundException  e) {System.out.println("load driver failed!");e.printStackTrace();}//connect dbtry {conn = DriverManager.getConnection(url, user, password);} catch (SQLException e) {System.out.println("connect failed!");e.printStackTrace();}		return conn;}
}

数据库表:

CREATE TABLE `user` (`id` int(11) NOT NULL auto_increment,`name` varchar(50) NOT NULL,`age` tinyint(10) NOT NULL,PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

Bean:

package Beans;public class UserBean {private int id; private String name; private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

Demo:

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import Beans.UserBean;public class main {public static void main(String[] args) throws SQLException {insert_test();del_test();}static void insert_test() throws SQLException{Connection conn = ConnectDb.Connect();//创建SQL执行工具 QueryRunner qRunner = new QueryRunner(); //执行SQL插入 int n = qRunner.update(conn, "insert into user(name,age) values('xxx',22)"); System.out.println("成功插入" + n + "条数据!"); //关闭数据库连接 DbUtils.closeQuietly(conn); 	} static void select_test() throws SQLException{Connection conn = ConnectDb.Connect();//创建SQL执行工具 QueryRunner qRunner = new QueryRunner(); @SuppressWarnings("unchecked")List<UserBean> list = (List<UserBean>) qRunner.query(conn, "select id,name,age from user", new BeanListHandler(UserBean.class)); //输出查询结果 for (UserBean user : list) { System.out.println(user.getAge()); } //关闭数据库连接 DbUtils.closeQuietly(conn); } static void update_test() throws SQLException{Connection conn = ConnectDb.Connect();//创建SQL执行工具 QueryRunner qRunner = new QueryRunner(); //执行SQL插入 int n = qRunner.update(conn, "update user set name = 'xxx',age=28"); System.out.println("成功更新" + n + "条数据!"); //关闭数据库连接 DbUtils.closeQuietly(conn); } static void del_test() throws SQLException{Connection conn = ConnectDb.Connect();//创建SQL执行工具 QueryRunner qRunner = new QueryRunner(); //执行SQL插入 int n = qRunner.update(conn, "DELETE from user WHERE name='xxx';"); System.out.println("删除成功" + n + "条数据!"); //关闭数据库连接 DbUtils.closeQuietly(conn); } 
}

补充个map的例子:

static void map_query(){Connection conn = ConnectDb.Connect();try {QueryRunner qr = new QueryRunner() ;ResultSetHandler rsh = new MapListHandler();String strsql = "select * from user";ArrayList result = (ArrayList)qr.query(conn ,strsql ,rsh);for(int i = 0 ; i < result.size() ; i++) {Map map = (Map)result.get(i);}} catch(Exception ex) {ex.printStackTrace(System.out);}
}

转自【http://blog.csdn.net/luxiaoyu_sdc/article/details/7361714】

这篇关于Apache DbUtils详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/619703

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装