Basic usage of dbutils

I used the connection pool of c3p0 here. Please refer to this link: The basic usage of c3p0

dbutils is just a tool class. If you don't use hibernate, you can consider using. Because it can greatly simplify our code

The jar package can be downloaded on the official website or here: The tool class of Java connecting database

Mainly used in QueryRunner and resultsethandler < T >

Query runner mainly uses

1. The query method is used for query

2.update method, used to add, delete and modify

3.batch method, used for batch processing

And ResultSetHandley can be understood as receiving results

Here's the code

package cn.bl.v4_DataSource.DBUtils;

import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Test;

import cn.bl.bean.Stud;
import cn.bl.v4_DataSource.c3p0.C3P0Utils;

public class Demo1 {
	
	//Query - encapsulate the results as list < map < string, Object > >
	@Test
	public void testQuery1() throws Exception {
		QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
		List<Map<String, Object>>list = runner.query(" select * from car ",new MapListHandler() );
		System.out.println(list);
		//2, id = 6}, {price=23456.22, cname = Honda, PID = 2, id = 7}, {price = 23456.22, C Name = QQ, PID = 4, id = 8}, {price = 12.0, CNAME = Maybach, pid=3, id=11}]
	}
	
	//Query - encapsulate as a list < student >
	@Test
	public void testQuery2() throws Exception {
		QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
		List<Stud> list = runner.query(" select * from stud", new BeanListHandler<>(Stud.class));
		System.out.println(list);
		//Age = 12, sex = 1], study [id = 24, name = immortal, age = 23, sex = 1], study [id = 39, n Ame = xiaolihua, age = 17, sex = 1], study [id = 50, name = Zhang Fei, age=30, sex=1], study [id = 49, name = Liu Bei, age = 34, sex = 1], study [id = 52, name = Zhang Fei, age=30, sex=1], study [id = 53, name = Zhang Fei, age=30, sex=1]]
	}
}
package cn.bl.v4_DataSource.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Random;

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

import cn.bl.v4_DataSource.c3p0.C3P0Utils;

public class Demo2 {

	@Test 
	public void testUpdate() throws Exception {
		QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
		int i = runner.update(" insert into stud(id,name,age,sex) values(50,'Zhang Fei',30,'1')");
		System.out.println(i);
		
		i = runner.update(" insert into stud(id,name,age,sex) values(?,?,?,?)", 49,"Liu Bei",34,"1");
		System.out.println(i);
	}
	
	@Test
	public void testBatch() throws SQLException {
		QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
		Random r = new Random();
		for(int i = 1;i<=100;i++) {
			Object[][]objects = new Object[][]{
					{"Tom"+i,r.nextInt(20)+10},
					{"Little plum"+i,r.nextInt(20)+10},
					{"Nancy"+i,r.nextInt(20)+10}
			};
			runner.batch(" insert into student(sname,age) values(?,?)",objects);
		}
	}
	
	/*
	 * Test transactions
	 */
	@Test
	public void testTx() {
		QueryRunner runner = new QueryRunner();
		Connection conn = C3P0Utils.getConnection();
		try {
			conn.setAutoCommit(false);
			String sql = " insert into stud(id,name,age,sex) values(52,'Zhang Fei',30,'1') ";
			runner.update(conn, sql);
			sql = " insert into stud(id,name,age,sex) values(53,'Zhang Fei',30,'1') ";
			runner.update(conn, sql);
			conn.commit();
			System.out.println("Transaction committed...");
		} catch (SQLException e) {
			System.out.println("Transaction rollback...");
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				conn.setAutoCommit(true);
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}

 

Keywords: Java SQL Apache Junit

Added by LordTyphon on Thu, 26 Dec 2019 22:46:04 +0200