DButils工具的使用

第一步:首先导入四个jar包

1. mysql-connector-java-5.1.13-bin.jar

2. commons-beanutils-1.9.3.jar

3. commons-logging-1.2.jar

4. commons-dbutils-1.7.jar

第二步:编写JavaBean

package com.ydwj.DBUtils;

public class Girl {
	private Integer girlID;
	private String girlName;
	private Integer girlAge;

	public Girl() {}

	public Girl(Integer girlID, String girlName, Integer girlAge) {
		super();
		this.girlID = girlID;
		this.girlName = girlName;
		this.girlAge = girlAge;
	}

	public String getGirlName() {
		return girlName;
	}

	public void setGirlName(String girlName) {
		this.girlName = girlName;
	}

	public Integer getGirlAge() {
		return girlAge;
	}

	public void setGirlAge(Integer girlAge) {
		this.girlAge = girlAge;
	}

	public Integer getGirlID() {
		return girlID;
	}
	public void setGirlID(Integer girlID) {
		this.girlID = girlID;
	}

	@Override
	public String toString() {
		return "Girl [girlID=" + girlID + ", girlName=" + girlName + ", girlAge=" + girlAge + "]";
	}

}

第三步:编写DBUtils方法测试类

query相关的方法

package com.ydwj.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.ydwj.utils.JDBCUtils;

public class Demo1 {
	public static void main(String[] args) {
//		query1();
//		query2();
//		query3();
//		query4();
//		query5();
//		query6();
		query7();
	}
	
	/**
	 *  获取单个girl对象
	 *  用ResultSetHandler
	 */
	public static void query1() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=2";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new ResultSetHandler() {

				@Override
				public Girl handle(ResultSet arg0) throws SQLException {
					Girl g = null;
					if (arg0.next()) {
						g = new Girl(arg0.getInt("girlID"), arg0.getString("girlName"), 
								arg0.getInt("girlAge"));
					}
					return g;
				}

			});
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}

	/**
	 * 获取单个girl对象
	 * 用BeanHandler
	 */
	public static void query2() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID = 3";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new BeanHandler<>(Girl.class));
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
	
	
	/**
	 * 获取多个girl对象,返回一个包含有Girl对象的List集合
	 * 使用BeanListHandler
	 */
	public static void query3() {
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		String sql = "select * from girl";
		
		try {
			List list = qr.query(conn, sql, new BeanListHandler<>(Girl.class));
			
			for (Girl girl : list) {
				System.out.println(girl);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
		}
		
	}

	/**
	 * 获取一个Girl对象
	 * 使用的方法,QueryRunner query(Connection conn, String sql, ResultSetHandler rs, Object... params)
	 */
	public static void query4() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=?";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new BeanHandler<>(Girl.class), 2);
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
		
	}

	
	/**
	 * 将数据库查询到的数据保存到一个Object类型的数组中
	 */
	public static void query5() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=2";
		QueryRunner qr = new QueryRunner();
		try {
			Object[] arr = qr.query(conn, sql, new ArrayHandler());
			System.out.println(Arrays.toString(arr));
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
		
	}
	
	
	/**
	 * 将数据库查询到的所有数据,每一个数据行做成一个数组,把所有的数组放入到一个List集合中
	 */
	public static void query6() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl";
		QueryRunner qr = new QueryRunner();
		try {
			List list = qr.query(conn, sql, new ArrayListHandler());
			for (Object[] objects : list) {
				System.out.println(Arrays.toString(objects));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
	
	public static void query7() {
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		String sql = "select * from girl";
		
		try {
			List> list = qr.query(conn, sql, new MapListHandler());
			for (Map map : list) {
				Set> entrySet = map.entrySet();
				for (Entry entry : entrySet) {
					System.out.println(entry);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
}

update相关的方法

package com.ydwj.DBUtils;

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

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import com.ydwj.utils.JDBCUtils;

public class Demo2 {
	/**
	 * 这里可以用到所有的增、删、改方法
	 */
	@Test
	public void testUpdate() {
		String sql = "delete from girl where girlID=1";
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			qr.update(conn, sql);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
			//也可以使用DBUtils提供的方法
			//DbUtils.close(conn);
		}
	}
	
	/**
	 * 批处理,减少访问数据库的次数,降低数据库的压力
	 */
	@Test
	public void testBatch() {
		String sql = "insert into girl(girlName,girlAge) values(?,?)";
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			qr.batch(conn, sql, new Object[][] {{"郭琪",22},{"刘茜茜",21}});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
		}
	}
}

 

你可能感兴趣的:(Java)