DBUtils增删改查

DBUtils 增删改查

		//dbutils 只是帮我们简化了CRUD 的代码, 但是连接的创建以及获取工作。 不在他的考虑范围
		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
	
		
		//增加
		queryRunner.update("insert into account values (null , ? , ? )", "aa" ,1000);
		
		//删除
		queryRunner.update("delete from account where id = ?", 5);
		
		//更新
		queryRunner.update("update account set money = ? where id = ?", 10000000 , 6);

查询

  1. 直接new接口的匿名实现类
QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
	

		Account  account =  queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>(){

			@Override
			public Account handle(ResultSet rs) throws SQLException {
				Account account  =  new Account();
				while(rs.next()){
					String name = rs.getString("name");
					int money = rs.getInt("money");
					
					account.setName(name);
					account.setMoney(money);
				}
				return account;
			}
			 
		 }, 6);
		
		System.out.println(account.toString());

  1. 直接使用框架已经写好的实现类。
//查询单个对象
QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
		//查询单个对象
		Account account = queryRunner.query("select * from account where id = ?", 
				new BeanHandler<Account>(Account.class), 8);
//查询多个对象

		QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
		List<Account> list = queryRunner.query("select * from account ",
				new BeanListHandler<Account>(Account.class));

ResultSetHandler 常用的实现类

以下两个是使用频率最高的

BeanHandler, 查询到的单个数据封装成一个对象
BeanListHandler, 查询到的多个数据封装 成一个List<对象>

ArrayHandler, 查询到的单个数据封装成一个数组
ArrayListHandler, 查询到的多个数据封装成一个集合 ,集合里面的元素是数组。

MapHandler, 查询到的单个数据封装成一个map
MapListHandler,查询到的多个数据封装成一个集合 ,集合里面的元素是map。

  • ColumnListHandler
  • KeyedHandler
  • ScalarHandler

DBUtils

简化了我们的CRUD , 里面定义了通用的CRUD方法。

	queryRunner.update();
	queryRunner.query

具体代码:

TestDBUtils .java类

package com.java.DBUtils;

import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestDBUtils {
	
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	//dbutils只是简化了代码,但是连接创建以及获取不在他的工作范围内
	QueryRunner queryRunner = new QueryRunner(dataSource);
	
	public void insert(){
		
		try {
			queryRunner.update("insert into stus values(10,?,?)", "aa", "333");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void delete() {
		
		try {
			queryRunner.update("delete from stus where id=?",10);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void update() {
		try {
			queryRunner.update("update stus set username=? where id=?", "wing",8);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void select() {
		
		/*try {
			//去执行查询,查询到的数据还是在哪个result里面,然后调用下面的handle方法,由用户手动去封装
			Bean bean = queryRunner.query("select * from stus where id=?",new ResultSetHandler<Bean>() {

				@Override
				public Bean handle(ResultSet result) throws SQLException {
					Bean bean = new Bean();
					while (result.next()) {
						String username = result.getString("username");
						String password = result.getString("password");
						
						bean.setUsername(username);
						bean.setPassword(password);
					}
					return bean;
				}
				
			},8);
			System.out.println(bean.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		try {
			//查询单个对象
			/*Bean bean = queryRunner.query("select * from stus where id=?", 
					new BeanHandler<Bean>(Bean.class), 3);
			System.out.println(bean.toString());*/
			
			List<Bean> list = queryRunner.query("select * from stus",
					new BeanListHandler<Bean>(Bean.class));
			for (Bean bean : list) {
				System.out.println(bean.toString());
			}
		} catch (Exception e) {
			
		}
		
	}
	public static void main(String[] args) {
		TestDBUtils demo = new TestDBUtils();
		//demo.insert();
		//demo.delete();
		//demo.update();
		demo.select();
	}
}

Bean.java类

package com.java.DBUtils;

public class Bean {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "Bean [username=" + username + ", password=" + password + "]";
	}
	
}

正在尝试写博客,把会的分享给你们,如有写的不好的地方,希望指点一下,谢谢!

猜你喜欢

转载自blog.csdn.net/Woo_home/article/details/88921494
今日推荐