Java Database DBUtils framework introduction, javadbutils

Source: Internet
Author: User

Java Database DBUtils framework introduction, javadbutils
1. JavaBean is a class that encapsulates data in common terms during development. Has the following features:
1. interface to be implemented: java. io. Serializable, which is usually omitted by lazy.
2. provide private fields: private field names;
3. Provides the getter/setter method: (The get and set methods must be implemented in the JavaBean class; otherwise, no data can be obtained)
4. Provide parameter-Free Structure
2. Introduction to three core functions of DBUtils:
1) QueryRunner provides APIs for SQL statement operations.
2) The ResultSetHandler interface is used to define how to encapsulate the result set after the select Operation.
3) DBUtils class, which is a tool class that defines how to close resources and transactions.
Iii. QueryRunner core class QueryRunner (DataSource ds): provides a data source (connection pool), and DBUtils automatically maintains the connection update (Stringsql, Object... params): Execute the update data query (String SQL, ResultSetHandler Rsh, Object... Params): executes the query.
Iv. ResultSetHandler result set processing class

ArrayHandler Encapsulate the first record in the result set into an Object [] array. Each element in the array is the value of each field in the record.
ArrayListHandler Encapsulate each record in the result set into an Object [] array and encapsulate these arrays in the List set.
BeanHandler Encapsulate the first record in the result set into a specified javaBean.
BeanListHandler Encapsulate each record in the result set into the specified javaBean, and encapsulate these javaBean in the List set.
ColumnListHandler Encapsulate the Field Values of the specified columns in a List set.
KeyedHandler Encapsulate each record in the result set to Map When this map set is used as the value of another Map, the key of the other Map set is the value of the specified field.
MapHandler Encapsulate the first record in the result set into the Map In the collection, the key is the field name, and the value is the field value.
MapListHandler Encapsulate each record in the result set into the Map Set, the key is the field name, and the value is the field value. In the List set, these maps are encapsulated.
ScalarHandler It is used for single data. For example, select count (*) from table operation.

V. Code implementation: 1. Add data
// Add data @ Testpublic void addUser () throws SQLException {// 1. Create the core class QueryRunnerQueryRunner qr = new QueryRunner (DBCPUtils. getDataSource (); // 2. Enter the SQL statement String SQL = "insert into student values (null ,?,?,?,?) "; // 3. assign a placeholder value: Object [] params = {" Zhao Xiaodong "," 22 "," male "," 123456 "}; // 4. Execute the add operation int rows = qr. update (SQL, params); if (rows> 0) {System. out. println ("Add Success! ") ;}Else {System. out. println (" Add Failed ");}}

2. modify data:
// Modify data @ Testpublic void Update () throws SQLException {// 1. method 1 // QueryRunner qr = new QueryRunner (DBCPUtils. getDataSource (); // String SQL = "update student set passwd =? Where name =? "; // Object [] params = {" 123456 "," michong "}; // place the placeholder value in the array // int rows = qr. update (SQL, params); // 2. The second method is QueryRunner qr = new QueryRunner (); String SQL = "update student set passwd =? Where name =? "; Int rows = qr. update (DBCPUtils. getConnection (), SQL, "12345678", "michong"); if (rows> 0) {System. out. println ("Update Success");} else {System. out. println ("Update Failed ");}}

3. query data and place it in the List set
// All queries, and put them in the List collection @ Testpublic void selectListALL () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select * from student"; // obtain the result set List
 
  
Students = qr. query (SQL, new BeanListHandler
  
   
(Student. class); System. out. println (students);} // a single query @ Testpublic void selectOne () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select * from student where id =? "; // Get a single result Student student = qr. query (SQL, new BeanHandler
   
    
(Student. class), 1); System. out. println (student );}
   
  
 
4. query data and put it in Map
// For a single query, use Map @ Testpublic void selectMap () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select * from student where id =? "; Map
 
  
Query = qr. query (SQL, new MapHandler (), 1); Set
  
   
> EntrySet = query. entrySet (); for (Entry
   
    
Entry: entrySet) {System. out. print (entry. getKey () + ":"); System. out. println (entry. getValue (); System. out. println ("--------------------") ;}// multiple queries, List
    
     
> @ Testpublic void selectMapAll () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select * from student"; List
     
      
> Maps = qr. query (SQL, new MapListHandler (); System. out. println (maps );}
     
    
   
  
 

5. Used for single data. For example, select count (*) from table operation.
@ Testpublic void selectSingleData () throws SQLException {QueryRunner qr = new QueryRunner (C3P0Utils. getDataSource (); String SQL = "select count (*) from student"; // obtain the number of queries long size = (long) qr. query (SQL, new ScalarHandler (); System. out. println (size );}

Related Article

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.