Result Set from BeanHandler and BeanListHandler

Posted by signs on Wed, 31 Jul 2019 18:43:32 +0200

Links to the original text: https://blog.csdn.net/smile_Running/article/details/87009755

DBUtils Toolkit
DBUtils is an open source JDBC tool class library provided by Apache. It is a simple encapsulation of JDBC and has a very low learning cost. Using dbutils can greatly simplify the workload of JDBC coding without affecting the performance of the program.

The QueryRunner class simplifies the SQL query. It can complete most of the database operations by combining it with the ResultSetHandler interface, which can greatly reduce the amount of coding. The ResultSetHandler interface provides the ability to convert data to another form as required.

I. Common implementation classes
1,BeanHandler
Encapsulate the first row of data in the result set into a corresponding JavaBean instance

For example:

@Test
public void testBeanHandler() {
	QueryRunner queryRunner = new QueryRunner();
	Connection conn = null;
	try {
		conn = JdbcUtils.getConnectionForC3P0();
		String sql = "select username,password,register_time,sex,user_role,id_card from user where username=?";
		User user = queryRunner.query(conn, sql, new BeanHandler<>(User.class), "Xiao Wang");
		System.out.println(user);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) {
			JdbcUtils.release(null, conn, null);
		}
	}
}

2,BeanListHandler
Encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in a List

For example:

@Test
public void testBeanListHandler() {
	QueryRunner queryRunner = new QueryRunner();
	Connection conn = null;
	try {
		conn = JdbcUtils.getConnectionForC3P0();
		String sql = "select username,password,register_time,sex,user_role,id_card from user";
		List<User> user = queryRunner.query(conn, sql, new BeanListHandler<>(User.class));
		System.out.println(user);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) {
			JdbcUtils.release(null, conn, null);
		}
	}
}

3,MapHandler
Encapsulate the first row of data in the result set as a Map

For example:

@Test
public void testMapHandler() {
	QueryRunner queryRunner = new QueryRunner();
	Connection conn = null;
	try {
		conn = JdbcUtils.getConnectionForC3P0();
		String sql = "select username,password,register_time,sex,user_role,id_card from user where username=?";
		Map<String, Object> user = queryRunner.query(conn, sql, new MapHandler(), "Xiao Wang");
		System.out.println(user);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) {
			JdbcUtils.release(null, conn, null);
		}
	}
}

4,MapListHandler
Encapsulate each row of data in the result set into a Map and store it in List
https://www.ximalaya.com/yinyue/25375793/
https://www.ximalaya.com/yinyue/25375783/
https://www.ximalaya.com/yinyue/25375779/
https://www.ximalaya.com/yinyue/25375770/
https://www.ximalaya.com/yinyue/25375764/
https://www.ximalaya.com/yinyue/25375758/
https://www.ximalaya.com/yinyue/25375752/
https://www.ximalaya.com/yinyue/25390775/
https://www.ximalaya.com/yinyue/25390676/
https://www.ximalaya.com/yinyue/25390685/
https://www.ximalaya.com/yinyue/25390689/
https://www.ximalaya.com/yinyue/25390696/
https://www.ximalaya.com/yinyue/25390698/
https://www.ximalaya.com/yinyue/25390704/
https://www.ximalaya.com/yinyue/25390713/
https://www.ximalaya.com/yinyue/25390715/
https://www.ximalaya.com/yinyue/25390720/
https://www.ximalaya.com/yinyue/25390435/
https://www.ximalaya.com/yinyue/25390724/
https://www.ximalaya.com/yinyue/25393616/
https://www.ximalaya.com/yinyue/25393622/
https://www.ximalaya.com/yinyue/25393628/
https://www.ximalaya.com/yinyue/25393185/
https://www.ximalaya.com/yinyue/25393198/
For example:

@Test
public void testMapListHandler() {
	QueryRunner queryRunner = new QueryRunner();
	Connection conn = null;
	try {
		conn = JdbcUtils.getConnectionForC3P0();
		String sql = "select username,password,register_time,sex,user_role,id_card from user";
		List<Map<String, Object>> user = queryRunner.query(conn, sql, new MapListHandler());
		System.out.println(user);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) {
			JdbcUtils.release(null, conn, null);
		}
	}
}

5,ScalarHandler
Returns a single value in the result set

For example:

@Test
public void testScalarHandler2() {
	QueryRunner queryRunner = new QueryRunner();
	Connection conn = null;
	try {
		conn = JdbcUtils.getConnectionForC3P0();
		String sql = "select register_time from user where id_card=?";
		Object register_time = queryRunner.query(conn, sql, new ScalarHandler(), "321...");
		System.out.println(register_time);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (conn != null) {
			JdbcUtils.release(null, conn, null);
		}
	}
}

6. Points for Attention
The result set obtained by BeanHandler and BeanListHandler does not exist (NULL). In this case, for example, NULL will result. Figure:

The reason for this is that the corresponding attribute name in the entity class (User) does not match the field (column name) of the database. The data of the database is as follows:



Regis_time, user_role, id_card and class variables registerTime, userRole, idCard can not be assigned, which is also a defect of BeanHandler, while other variables such as MapHandler and ScalarHandler do not.

The reason is that the bottom layer of MapHandler and ScalarHandler uses reflection to get the column names of metadata, so it can get the results.

Author: Weiweimiao
Source: CSDN
Original: https://blog.csdn.net/smile_Running/article/details/87009755
Copyright Statement: This article is the original article of the blogger. Please attach a link to the blog article for reprinting.

Topics: SQL JDBC Database Apache