Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

DBUtils introduction

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. DBUtils introduces apache

What is DBUtils and what it does

DBUtils is a database operation utility in Java programming, which is small, simple and practical.

DBUtils encapsulates the operation of JDBC, simplifies the operation of JDBC, and can write less code.

1. For the read operation of the data table, he can convert the results into Java collections such as List,Array,Set, which is convenient for programmers to operate.

two。 It is also easy to write data tables (just write sql statements)

3. You can use data sources, JNDI, database connection pooling and other technologies to optimize performance-reuse database connection objects that have already been built

Second, the three core objects of DBUtils

QueryRunner class

ResultSetHandler interface

DBUtils class

QueryRunner class

API that provides operations on sql statements in QueryRunner

It has three main ways.

Query () / / is used to execute select

Update () / / is used to execute insert update delete

Batch () / / batch

ResultSetHandler interface

Used to define how to encapsulate the result set after a select operation

DBUtils class

It is a utility class that defines methods to close resources and transactions.

III. Getting started with Dbutils

Import jar package

Create a QueryRunner object

Execute a select statement using the query method

Encapsulate result sets using ResultSetHandler

Use the DBUtils class to release resources

Steps to get started with DBUtils:

Import jar package

Note: c3p0 and mysql driver jar should also be imported.

Create a QueryRunner object

Execute a select statement using the query method

Encapsulate result sets using ResultSetHandler

Use the DBUtils class to release resources

4. QueryRunner object

Constructor: how much is the flow of people in Wuxi http://www.xaytsgyy.com/

New QueryRunner (): its transactions can be controlled manually

That is to say, there should be a Connection object in the parameters of the methods called by this object (such as query, update, batrch)

New QueryRunner (DataSource ds): its transaction is automatically controlled by a sql transaction.

No Connection object is required in the parameters of the methods called by this object (such as query, update, batrch)

Do basic CRUD operations: practice

Public class TestQueryRunner {

@ Test

Public void testInsert () throws SQLException {

QueryRunner qr = new QueryRunner (C3P0Util.getDataSource ())

Qr.update ("insert into student (NAME, birthday) values", "bbb", new Date ())

}

@ Test

Public void testUpdate () throws SQLException {

QueryRunner qr = new QueryRunner (C3P0Util.getDataSource ())

Qr.update ("update student set name=?, birthday=? where id=?", "ccc", new Date (), 1)

}

@ Test

Public void testDelete () throws SQLException {

QueryRunner qr = new QueryRunner (C3P0Util.getDataSource ())

Qr.update ("delete from student where id=?", 1)

}

@ Test

Public void testBatch () throws SQLException {

QueryRunner qr = new QueryRunner (C3P0Util.getDataSource ())

Object [] [] params = new Object [10] []; / / High dimension indicates how many sql statements to execute

For (int I = 0; I < params.length; iTunes +) {

Params [I] = new Object [] {"dish 10" + I, "123"," c10@163.com ", new Date ()}

}

Qr.batch ("insert into user (username, password, email, birthday) values", params)

}

}

5. ResultSetHandler interface

All result processors under ResultSetHandler

ArrayHandler: it is suitable to take 1 record. Encapsulate the values of each column of the record into an array Object []

ArrayListHandler: suitable for fetching multiple records. Encapsulate the values of each column of each record into an array Object [], and encapsulate the array into a List

ColumnListHandler: take a column of data. Encapsulated into List

KeyedHandler: take multiple records. Each record is encapsulated in a Map, and then the Map is encapsulated in another Map. Key is the specified field value.

MapHandler: it is suitable to take 1 record. Put the column names and values of the current record into a Map

MapListHandler: suitable for fetching multiple records. Encapsulate each record in a Map, and then encapsulate the Map in the List

ScalarHandler: suitable for single row and single column data

BeanHandler

BeanListHandler

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report