Dbutils Tool Class
I. INTRODUCTION
Dbutils is an open source database tool class for Apache organizations.
Two. Steps to use
①. Creating a Queryrunner Object
②. Call the update () method or the query () method to execute the SQL statement
Three. Construction method and static method
Queryrunner class
1. Construction method
①. Non-parametric construction
Queryrunner qr =new queryrunner ();
When using an parameterless construct, the Update method and the query method are called using overloaded forms with the connection type parameter
②. Construction of a reference
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 to delete and modify the operation of the statement
Parameter description:
Parameter one: Connection pool object ( This is used when no parameter is constructed)
Parameter two: SQL statement
Parameter three: variable parameter (that is, the value of the SQL placeholder)
Return value: The number of rows affected by the return of type int
Simple Update Demo
1 Public classDemo {2 Public Static voidMain (string[] args)throwsException {3 /*4 * Show the Update () method with the parameter structure5 * 6 * The jar package must be imported first7 * Configure the C3P0 configuration file and prepare the C3p0 tool class8 * Then create the Queryrunner object9 * Call the Update methodTen * Final processing results One */ AQueryrunner QR =NewQueryrunner (C3p0utils.getdatasource ()); - intRe = qr.update ("Update user set name=?") Where uid=? "," Zhang San ", 2); - if(re>0){ theSYSTEM.OUT.PRINTLN ("Modified successfully"); -}Else { -System.out.println ("Modify failed"); - } + } -}
Attach a simple C3P0 tool class
1 Public classC3p0utils {2 Private StaticDataSource datasource=NewCombopooleddatasource ();3 /**4 * Get DataSource Implementation class object5 * @return6 */7 Public StaticDataSource Getdatasource () {8 returnDataSource;9 }Ten /** One * Get Connected A * @return - * @throwsException - */ the Public StaticConnection getconnection ()throwsexception{ - returndatasource.getconnection (); - } -}
②.query (Connection con, String sql, Param ...)
This method is used for out-of-query operations
Parameter description:
Parameter one: Connection database Connection object, use with parameter constructs can not
Parameter two: SQL statement
Parameter three: Indicates how the result set is handled (Resultsethandler interface)
Arrayhandler: Indicates that the data in the first row of the result set is stored in an array
Arraylisthandler the data in each row of the result set into an array, and multiple arrays are stored in the collection list<object[]>
Beanhandler means the data in the first row of the result set is stored in the Java Bean object
Beanlisthandler means that data from each row of the result set is stored in a Java Bean object, and multiple objects are stored in the collection
Columnlisthandler indicates that a column of data is stored in a collection
Maphandler represents the data in the first row of the result set in the Map collection: Key: Column Name value: Column value
Maplisthandler indicates that the data for each row of the result set is stored in the map collection multiple maps are stored in the List collection list<map<,>>
Scalarhandler gets a value: count (*) sum (price)
Parameter four: variable parameter (that is, the value of the SQL placeholder)
Demo using Beanlisthandler processing mode:
1 Public voidDemo1 ()throwsexception{2Queryrunner QR =NewQueryrunner (Myc3p0utils.getdatasource ());3list<car> list = Qr.query ("select * from Car where price<20 order BY price Desc",NewBeanlisthandler<> (Car.class));4 for(Car car:list) {5 System.out.println (car);6 }7 8}
Writing of the JavaBean class:
1 Public classCar {2 Private intCID;3 PrivateString cname;4 PrivateString Company;5 PrivateString grade;6 Private DoublePrice ;7 @Override8 PublicString toString () {9 return"Car [cid=" + CID + ", cname=" + CNAME + ", company=" + company + ", grade=" + Grade + ", price=" + PriceTen+ "]"; One } A Public intgetcid () { - returnCID; - } the Public voidSetcid (intCID) { - This. CID =CID; - } - PublicString Getcname () { + returncname; - } + Public voidsetcname (String cname) { A This. CNAME =cname; at } - PublicString Getcompany () { - returnCompany ; - } - Public voidSetcompany (String company) { - This. Company =Company ; in } - PublicString Getgrade () { to returngrade; + } - Public voidSetgrade (String grade) { the This. grade =grade; * } $ Public DoubleGetPrice () {Panax Notoginseng returnPrice ; - } the Public voidSetprice (DoublePrice ) { + This. Price =Price ; A } the PublicCar (intCID, String cname, string company, string grade,DoublePrice ) { + Super(); - This. CID =CID; $ This. CNAME =cname; $ This. Company =Company ; - This. grade =grade; - This. Price =Price ; the } - PublicCar () {Wuyi Super(); the //TODO auto-generated Constructor stub - } Wu -}
Java Learning Notes Dbutils tool class