Java Foundation JDBC Nine: Introduction and use of Dbutils

Source: Internet
Author: User

Dbutils is an open Source Tool class library provided by the Apache Organization for simple JDBC encapsulation, which simplifies the development of JDBC applications without compromising program performance.

Steps to use:

A: Gets the object that can execute the SQL statement.  PublicQueryrunner ();  PublicQueryrunner (DataSource DS);//If a connection pool object is needed, it is automatically fetched from the database connection pool.B: Execute the SQL statement to get the result set. Query (String SQL,ResultsethandlerRSH, Object ... values);//Execute Query StatementResultsethandler://is an interface that represents how the result set is handled, the parameters passed in, and the return values are different.    intUpdate (String sql,object ... values);//Execute UPDATE statementC: Operation result set.//the underlying of a mutable parameter is actually a: array.subclass of Resultsethandler interface: Arrayhandler turns the first row of the result set into an array of objects object[] Arraylisthandler List<Object[]>turns each row of data in the result set into an array of objects, and then stores the array of objects that are turned into a list beanhandler encapsulates the first row of data in the result set into a corresponding business class instance //new beanhandler< type of Business class > (bytecode file object for business Class); //new Beanhandler<ledger> (ledger.class);Beanlisthandler encapsulates each row of data in the result set into a corresponding business class instance, and then stores multiple business class instance objects in the list. //new beanlisthandler< type of Business class > (bytecode file object for business Class); //new Beanlisthandler<ledger> (ledger.class);Maphandler encapsulates the first row of data in the result set into a map, key is the column name String, and value is the corresponding Obje Maplisthandler each row of data in the result set is encapsulated into A map, and then store multiple maps to list Columnlisthandler the data from the specified columns in the result set to the list collection//new columnlisthandler< The type of the column is written in reference type > (column name); //New Columnlisthandler<double> ("money");Scalarhandler for single-valued queries such as SELECT COUNT (*) from//one column at a line//new scalarhandler< The type of the column is written in reference type > (column name); //New Scalarhandler<long> ("counts"); How many rows are there for the data that satisfies the criteriaKeyedhandler encapsulates each row of data in the result set as a Map (map<string,object>), and then save the map to a map with the key Keyedhandler the specified column, or the field of the first query if not specified. /*1 2 3 4 Map < ;            String,map<string,object>> map= qr.query (conn, SQL, new keyedhandler<string> ("lid")); 1: Represents the specified column name passed in (the requirement cannot be duplicated, the general pass is the primary key) 2: Represents the database table of each row of data (column name do key, the specific data to do value) 3: Represents the data type of the specified column passed in 4: generation The table is the specified column that is passed in*/

To perform an update operation using dbutils:

/*** Use Dbutils to perform update operations * Import jar package before use*/ Public classTest06 { Public Static voidMain (string[] args) {//gets the object that executes the SQL statement//in this constructor, we need to pass a connection pool object. We use the C3P0 simple tool class previously encapsulated to get a connection pool objectQueryrunner QR =NewQueryrunner (C3p0simpleutils.getdatasource ()); //Execute SQL statement get return resultString sql = "UPDATE users SET PSW =?" WHERE uid =? "; Try{object[] obj= {NULL, "Ya", "Hasagei"}; intnum =qr.update (sql, obj); SYSTEM.OUT.PRINTLN (Num> 0? "Add Success": "Add Failed"); } Catch(SQLException e) {e.printstacktrace (); }        //no more close, because Dbutils did it for us.    }}

To perform a query operation using dbutils:

Java Foundation JDBC Nine: Introduction and use of Dbutils

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.