Dbutils Component of Learning notes

Source: Internet
Author: User
3. Dbutils Components 3.1 Introduction

Custom JDBC Persistence layer framework, features are not particularly powerful. Learn the good JDBC components, dbutils components, which are lightweight packages for JDBC connection data.

L Commons-dbutils is an open-source JDBC Tool class library provided by the Apache organization, a simple encapsulation of JDBC, very low learning costs, and the use of dbutils can greatly simplify the workload of JDBC coding without affecting program performance. So Dbutils became the first choice for a lot of companies that don't like hibernate.

L Project uses the Dbutils component, needs to introduce: Commons-dbutils-1.2.jar

3.2 API Introduction

The core classes provided by the |--dbutils component: The main offer is to close the connection and load the driver-related methods

The |--queryrunner component (most) core processing class encapsulates the basic operation of JDBC: query, update, batch.

1. Update method

2. Batch processing method

3. Query related methods

/**************** Update Method ***********************/

int update (STRINGSQL) Direct update

int update (stringsql, Object param) with a conditional update

int update (STRINGSQL, object[] params) with multiple conditional updates

int[] Batch (String SQL) batch

int update (CONNECTIONCON, String sql)

int update (Connectioncon, Object param)

int update (Connectioncon, object[] params)

|--resultsethandler result set processor

/**************** Query Method ***********************/

| |--arrayhandler query returns the first row of the result set, which ultimately returns a object[]

|--Arraylisthandler GET all the results and return list<object[]>

|--Beanhandler The first line of the query result, encapsulated as the specified JavaBean object

|--Beanlisthandler encapsulates each row of the result set as the specified JavaBean object, and then adds the object to the ArrayList collection

|--Columnlisthandler to query the specified columns of all records, add the collection, or add the values of the first column to the collection if no columns are specified

| |-keyedhandler query returns the Map collection, takes the specified column as the key, encapsulates each record as the value of the Map collection, the final result map< the specified column as the key,map< column name, the column value >>map;

|--Maphandler, encapsulating the results as a map

|--Maplisthandler encapsulates each row of results as a map, and then adds the map to the list collection

|--Scalarhandler Returns the first row of the result set, the aggregate function 3.3 dbutilsquery

/**

* Inquiry

* @author Administrator

*

*/

Public class Dbutilsquery {

Create a data source

Static DataSource ds;

Static {

ds = new combopooleddatasource ();

}

1. Arrayhandler: The query returns the first row of the result set, and the final return is a object[]

@Test

Public void Queryone () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Execute Query

object[] obj = (object[]) runner.query (SQL, new Arrayhandler ());

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

2. Arraylisthandler: Get all the results and return list<object[]>

@Test

Public void Queryall () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Execute Query

list<object[]> obj = (list<object[]>) runner.query (SQL, new Arraylisthandler ());

System.out.println (obj.get (0) [1]);

Catch (SQLException e) {

E.printstacktrace ();

}

}

3. Beanhandler: Encapsulates the first line of the query result to the specified JavaBean object

@Test

Public void Getonebean () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Object obj = runner.query (sql, new Beanhandler (ordermeal). Class));

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

4. Beanlisthandler: Encapsulates each row of the result set as the specified JavaBean object, and then adds the object to the ArrayList collection

@Test

Public void Getallbean () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Object obj = runner.query (sql, new Beanlisthandler (ordermeal). Class));

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

5. Columnlisthandler to query the specified columns of all records, add the collection, or add the values of the first column to the collection if no columns are specified

@Test

Public void GetColumn () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Object obj = runner.query (sql, new columnlisthandler ("user"));

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

6. Keyedhandler: The query returns the Map collection, takes the specified column as key, encapsulates each record as value, and the final result map< the specified column as key,map< column name, column value >> Map;

@Test

Public void getkeyed () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

map<integer,map<string,string>> obj = (Map<integer, map<string, string>>) runner.query (SQL, New Keyedhandler ("user"));

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

9. Scalarhandler: Returns the first column of the first row of the query result

@Test

Public void Getoneone () {

String sql = "SELECT * from admin;";

To create a core processing class

Queryrunner runner = new Queryrunner (DS);

Try {

Object obj = runner.query (sql, new scalarhandler ("meal"));

System.out.println (obj);

Catch (SQLException e) {

E.printstacktrace ();

}

}

}

3.4 dbutilsupdate

/**

* 1. Update

* 2. Batch Processing

* @author Administrator

*

*/

Public class dbutilsupdate {

Create a data source

Static DataSource ds;

Static {

ds = new combopooleddatasource ();

}

1. Delete: (insert, modify, delete), specify Connection object action

@Test

Public void Update () {

String sql = "DELETE from order_meal WHERE id=1";

To create a dbutils core processing class object

Queryrunner query = new queryrunner ();

Try {

Loading drivers using the Dbutils class

Dbutils.loaddriver ("Com.mysql.jdbc.Driver");

Get connection

Connection con =

Drivermanager.getconnection ("Jdbc:mysql:///day17", "root", "root");

Delete operation

Query.update (Con,sql);

Close connection

Dbutils.close (con);

Catch (SQLException e) {

E.printstacktrace ();

}

}

1. Delete, specify the data source when creating the Queryrunner object, and do not specify the connection object (recommended) When you do JDBC operation

@Test

Public void Update2 () {

String sql = "DELETE from order_meal WHERE id=2";

To create a dbutils core processing class object

Queryrunner query = new Queryrunner (DS);

Try {

Delete operation

Query.update (SQL);

Catch (SQLException e) {

E.printstacktrace ();

}

}

@Test

Public void Update3 () {

String sql = "DELETE from order_meal WHERE id=?" or id=? ";

To create a dbutils core processing class object

Queryrunner query = new Queryrunner (DS);

Try {

Object[] paramvalue = {4,5};

Delete operation

Query.update (Sql,paramvalue);

Shut down

dbutils.closequietly (NULL, NULL, NULL);

Catch (SQLException e) {

E.printstacktrace ();

}

}

@Test

2. Batch processing-delete

Public void Delbatch () {

String sql = "DELETE from order_meal WHERE id=?";

To create a dbutils core processing class object

Queryrunner runner = new Queryrunner (DS);

&n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.