DBUtils几种使用方法详解

public class TestDBUtils {
	@Test
	public void findOne() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people where id = 1";
		People p = null;
		
		try {
			p = qr.query(conn, sql, new ResultSetHandler() {

				@Override
				public People handle(ResultSet arg0) throws SQLException {
					People p1 = null;
					if (arg0.next()) {
						int id = arg0.getInt("id");
						String name = arg0.getString("name");
						int age = arg0.getInt("age");
						
						p1 = new People(id, name, age);
					}
					return p1;
				}
				
			});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		System.out.println(p);
				
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void findOneUseArgument() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people where id = ?";
		People p = null;
		
		try {
			p = qr.query(conn, sql, 4, new ResultSetHandler() {

				@Override
				public People handle(ResultSet arg0) throws SQLException {
					People p1 = null;
					if (arg0.next()) {
						int id = arg0.getInt("id");
						String name = arg0.getString("name");
						int age = arg0.getInt("age");
						
						p1 = new People(id, name, age);
					}
					return p1;
				}
			});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		System.out.println(p);
	}
	
	@Test
	public void findOneFinally() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people where id = ?";
		People p = null;
		
		try {
			p = qr.query(conn, sql, new BeanHandler(People.class), 6);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		System.out.println(p);
	}
	
	@Test
	public void findAllList() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people";
		List query = null;
		
		try {
			query = qr.query(conn, sql, new BeanListHandler(People.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		for (People people : query) {
			System.out.println(people);
		}
	}
	
	/**
	 * 将一条数据处理成数组形式展示
	 */
	@Test
	public void findInfoArray() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people";
		Object[] query = null;
		try {
			query = qr.query(conn, sql, new ArrayHandler());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		for (Object object : query) {
			System.out.println(object);
		}
	}
	
	/**
	 * 所有的数据,单条数据是一个数组,放到一个List集合中
	 */
	@Test
	public void findAllArray() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people";
		List query2 = null;
		try {
			query2 = qr.query(conn, sql, new ArrayListHandler());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		for (Object[] objects : query2) {
			System.out.println(Arrays.toString(objects));
		}
	}
	
	@Test
	public void findAllMap() {
		Connection conn = JdbcUtils.getConnection();

		// DBUtils核心类对象
		QueryRunner qr = new QueryRunner();
		
		String sql = "select * from people";
		List> query = null;
		try {
			 query = qr.query(conn, sql, new MapListHandler());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtils.close(conn);
		}
		
		for (Map map : query) {
			System.out.println(map);
		}
	}
}

你可能感兴趣的:(学习)