黑马day12 DbUtils的介绍
简单介绍:
DbUtils为不喜欢hibernate框架的钟爱。它是线程安全的,不存在并发问题。
使用步骤:
1. QueryRunner runner=new QueryRunner(这里写数据源...如c3p0的数据元new ComboPooledDataSource()或者dbcp的数据元);
2.使用runner的方法假设要增删改就使用update(String sql,Object ... params)
sql:传递的sql语句
params:可变參数。为sql语句中的?所取代的參数
使用runner的方法要查询使用runner的query(String sql,ResultSetHandle handle ,Object ... params)
sql:传递的sql语句
handle :一个接口要是实现当中的handle方法
params:可变參数,为sql语句中的?所取代的參数
案例:
package com.itheima.dbutils; import java.sql.ResultSet;
import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.junit.Test; import com.itheima.domain.Account;
import com.mchange.v2.c3p0.ComboPooledDataSource; public class DbUtilsDemo1 {
@Test
public void add() throws SQLException{
QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
runner.update("insert into account values(null,?,? )", "韩玮",7000);
}
@Test
public void update() throws SQLException{
QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
runner.update("update account set money=? where name=? ", 9000,"李卫康");
}
@Test
public void delete() throws SQLException{
QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
runner.update("delete from account where name=? ","韩玮");
}
@Test
public void query() throws SQLException{
final Account account=new Account();
QueryRunner runner=new QueryRunner(new ComboPooledDataSource());
runner.query("select * from account where name =?", new ResultSetHandler<Account>(){
@Override
public Account handle(ResultSet rs) throws SQLException {
while(rs.next()){
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
}
return account;
} }, "李卫康");
System.out.println(account.getMoney());
}
}
执行结果查询数据库:
mysql> select * from account;
+----+--------+-------+
| id | name | money |
+----+--------+-------+
| 1 | 李卫康 | 9000 |
+----+--------+-------+
1 row in set
mysql> select * from account;
+----+--------+-------+
| id | name | money |
+----+--------+-------+
| 1 | 李卫康 | 10000 |
+----+--------+-------+
1 row in set
mysql> select * from account;
+----+--------+-------+
| id | name | money |
+----+--------+-------+
| 1 | 李卫康 | 10000 |
| 5 | 程崇树 | 5000 |
+----+--------+-------+
黑马day12 DbUtils的介绍的更多相关文章
- day18(JDBC事务&连接池介绍&DBUtils工具介绍&BaseServlet作用)
day18总结 今日思维导图: 今日内容 事务 连接池 ThreadLocal BaseServlet自定义Servlet父类(只要求会用,不要求会写) DBUtils à commons-dbuti ...
- (二十三)Dbutils 工具介绍
目录 Dbutils简介 API 介绍 Dbutils 已实现的结果集处理器 : Dbutils简介 commons-dbutis 是Apache 组织提供的一个开源JDBC工具类库,它对JDBC进行 ...
- JDBC 学习笔记(四)—— 自定义JDBC框架+Apache—DBUtils框架+事务管理+操作多表
本文目录: 1.自定义JDBC框架 ——数据库元数据:DataBaseMetaData 2.自定义JDBC框架 ——数据库元数据:DataBaseMetaData ...
- 5月16日 python学习总结 DBUtils模块、orm 和 cookie、session、token
一.DBUtils模块 介绍 The DBUtils suite is realized as a Python package containing two subsets of modules, ...
- DBubtil的使用
1.什么是O-R Mapping(对象-关系映射) 常用O-R Mapping映射工具 Hibernate(全自动框架) Ibatis(半自动框架/SQL) Commons DbUti ls(只是对J ...
- Apache- DBUtils框架学习
一.DBUtils DBUtils 的介绍 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,,DBUtils封装了对JDBC的操作,简 ...
- 韩顺平JDBC学习笔记
第一节 JDBC概述 1.1 JDBC原理图 Java不可能具体地去操作数据库,因为数据库有许多种,直接操作数据库是一种很低效且复杂的过程. 因此,Java引入JDBC,规定一套操作数据库的接口规范, ...
- Dbutils学习(介绍和入门)
一:Dbutils是什么?(当我们很难理解一个东西的官方解释的时候,就让我们记住它的作用) Dbutils:主要是封装了JDBC的代码,简化dao层的操作. 作用:帮助java程序 ...
- xUtils介绍 -- DbUtils、ViewUtils、HttpUtils、BitmapUtils
转载注明出处:https://github.com/wyouflf/xUtils xUtils简单介绍 xUtils 包括了非常多有用的android工具. xUtils 支持大文件上传,更全面的ht ...
随机推荐
- spring4声明式事务—02 xml配置方式
1.配置普通的 controller,service ,dao 的bean. <!-- 配置 dao ,service --> <bean id="bookShopDao& ...
- bzoj 4242 水壶 (多源最短路+最小生成树+启发式合并)
4242: 水壶 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 1028 Solved: 261[Submit][Status][Discuss] ...
- 课下测试ch17&ch18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
- Xcode 几个图标解释
File.让您指定串联图的常规信息. Quick Help.提供有关对象的实用文稿. Identity.让您指定对象的自定类并定义其辅助功能属性. Attributes.让您自定对象的可视化属性. S ...
- no device found for connection ‘ System eth0′
解决办法: 1.删除/etc/udev/rules.d/70-persistent-net.rules文件,重启系统. 2.如果上面的不起作用,那么去看ifcfg-eth0文件中的HWADDR是否正确 ...
- JS实现背景透明度可变,文字不透明的效果
最近项目里需要实现这么个功能,类似网游中的聊天框,背景都是透明的,但是文字是不透明.所以如果简单的使用opacity(非IE)和alpha滤镜(IE)是无法实现这个效果的,会造成全部透明. 解决办法如 ...
- Disable File System Redirector For Windows x64 (Python recipe)(转)
This disables the Windows File System Redirector.When a 32 bit program runs on a 64 bit operating sy ...
- IntelliJ IDEA使用教程三 SVN的集成与使用
注意: 虽然IDEA已经集成了svn客户端,但还是习惯使用第三方svn客户端,比如: TortoiseSVN. 就是因为使用的是第三方客户端,所以和IDEA集成的时候就出现了一个特别大的坑,因为svn ...
- datagrid在MVC中的运用07-实现Master-Detail(使用PartialView)
本文主要体验用jQuery Easyui的datagrid来实现Master-Detail主次表.谢谢Kevin的博文,助我打开了思路. 主表显示所有的Category,当点击主表的展开按钮,显示该C ...
- 【centOS】centos7 查看和关闭防火墙
查看防火墙状态 firewall-cmd --state running代表防火墙正在运行 停止firewall systemctl stop firewalld.service 禁止firewall ...