Details of the java study Notes DBUtils toolkit

  • 2020-12-10 00:42:13
  • OfStack

DBUtils toolkit

1. Introduction

DBUtils is the open source database tool class of the Apache organization.

2. Use the steps

. Create the QueryRunner object

Call the update() method or query() method to execute the sql statement

3. Construction method and static method

QueryRunner class

1. Construction method

1. No parameter structure


QueryRunner qr =new QueryRunner();

When using unparameterized constructs, the update and query methods are called using overloaded forms with arguments of type Connection

(2). Structure with parameters


QueryRunner qr= new QueryRunner(DataSource dataSource);

This parameter is the connection pool object

2. Static methods

int update(Connection con,String sql,Param);

This method is used for adding or deleting statements

Parameter introduction:

Parameter 1: connection pool object (this is used when no arguments are constructed)

Parameter 2: sql statement

Argument 3: Variable argument (which is the value of the sql placeholder)

Return value: The number of rows affected is returned for type int

Simple update demo


public class Demo {
 public static void main(String[] args) throws Exception {
  /*
   *  Demonstrates construction with parameters update() methods 
   * 
   *  First you have to import jar package 
   *  configured C3P0 Configuration file with ready C3P0 Utility class 
   *  Then create a QueryRunner object 
   *  call update methods 
   *  Final processing result 
   */
  QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
  int re = qr.update("update user set name=? where uid=?"," zhang 3",2);
  if(re>0){
   System.out.println(" Modify the success ");
  }else {
   System.out.println(" Modify the failure ");
  }
 }
}

Attach a simple C3P0 utility class


public class C3P0Utils {
 private static DataSource dataSource=new ComboPooledDataSource();
 /**
  *  To obtain DataSource Implementation class object 
  * @return
  */
 public static DataSource getDataSource(){
  return dataSource;
 }
 /**
  *  Obtain a connection 
  * @return
  * @throws Exception
  */
 public static Connection getConnection()throws Exception{
  return dataSource.getConnection();
 }
}

(Connection con, String sql,Param...)

This method is used for outgoing query operations

Parameter introduction:

Parameter 1: Connection database connection object, which may not be used when using parameter construction

Parameter 2: sql statement

Parameter 3: represents how the result set is handled (ResultSetHandler interface)

ArrayHandler: Means to store the data in line 1 of the result set into an array

ArrayListHandler stores data for each row of the result set into an array, and multiple arrays into collection List < Object[] >

BeanHandler means to store the data in line 1 of the result set into the Java Bean object

BeanListHandler means that each row of the result set is stored in the Java Bean object, and multiple objects are stored in the collection

ColumnListHandler means to store data for a column in a collection

MapHandler means to store the data in row 1 of the result set into the Map collection: key: column name value: the value of the column

MapListHandler means that each row of the result set is stored in the Map set and more than one Map is stored in the List set < Map < , > >

ScalarHandler gets 1 value: count(*) sum(price)

Argument 4: Variable argument (which is the value of the sql placeholder)

demo using BeanListHandler processing:


public void demo1() throws Exception{
  QueryRunner qr = new QueryRunner(MyC3P0Utils.getDataSource()); 
  List<Car> list = qr.query("select * from car where price<20 order by price desc", new BeanListHandler<>(Car.class));
  for (Car car : list) {
   System.out.println(car);
  }
 
 }

javaBean class writing:


public class Car {
 private int cid;
 private String cname;
 private String company;
 private String grade;
 private double price;
 @Override
 public String toString() {
  return "Car [cid=" + cid + ", cname=" + cname + ", company=" + company + ", grade=" + grade + ", price=" + price
    + "]";
 }
 public int getCid() {
  return cid;
 }
 public void setCid(int cid) {
  this.cid = cid;
 }
 public String getCname() {
  return cname;
 }
 public void setCname(String cname) {
  this.cname = cname;
 }
 public String getCompany() {
  return company;
 }
 public void setCompany(String company) {
  this.company = company;
 }
 public String getGrade() {
  return grade;
 }
 public void setGrade(String grade) {
  this.grade = grade;
 }
 public double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
 public Car(int cid, String cname, String company, String grade, double price) {
  super();
  this.cid = cid;
  this.cname = cname;
  this.company = company;
  this.grade = grade;
  this.price = price;
 }
 public Car() {
  super();
  // TODO Auto-generated constructor stub
 }
}

Related articles: