Brief introduction:
Dbutils is not fond of the hibernate framework, it is thread-safe and there is no concurrency problem.
Steps to use:
1. Queryrunner runner=new Queryrunner (write data source here ...) such as C3P0 's data element new Combopooleddatasource () or DBCP's data element);
2. Use the Runner method if you want to delete or change the use of update (String sql,object ... params)
SQL: Passed SQL statements
Params: A variable parameter that is substituted for a SQL statement
Use the Runner method to query queries using runner (String Sql,resultsethandle handle, Object ... params)
SQL: Passed SQL statements
Handle: An interface that implements the handle method
Params: A variable parameter that is substituted for a SQL statement
Case:
Package Com.itheima.dbutils;import Java.sql.resultset;import Java.sql.sqlexception;import Org.apache.commons.dbutils.queryrunner;import Org.apache.commons.dbutils.resultsethandler;import org.junit.Test; Import Com.itheima.domain.account;import Com.mchange.v2.c3p0.combopooleddatasource;public class DbUtilsDemo1 {@ testpublic void Add () throws Sqlexception{queryrunner runner=new Queryrunner (New Combopooleddatasource ()); Runner.update ("INSERT into account values (null,?,?)", "Anlu", 7000);} @Testpublic void Update () throws Sqlexception{queryrunner runner=new Queryrunner (New Combopooleddatasource ()); Runner.update ("Update account set money=?") Where Name=? ", 9000," Li Weikang ");} @Testpublic void Delete () throws Sqlexception{queryrunner runner=new Queryrunner (New Combopooleddatasource ()); Runner.update ("Delete from account where name=?", "Anlu");} @Testpublic void Query () throws Sqlexception{final account Account=new account (); Queryrunner runner=new Queryrunner (New Combopooleddatasource ()); Runner.query ("SELECT * FRom account where name =? ", new Resultsethandler<account> () {@Overridepublic account handle (ResultSet Rs) throws SQL Exception {while (Rs.next ()) {Account.setid (Rs.getint ("id")); Account.setname (rs.getstring ("name")); Account.setmoney (rs.getdouble ("money")); return account;}}, "Li Weikang"); System.out.println (Account.getmoney ());}}
Run the results query database:
Mysql> select * from account;
+----+--------+-------+
| ID | name | Money |
+----+--------+-------+
| 1 | Li Weikang | 9000 |
+----+--------+-------+
1 row in Set
Mysql> select * from account;
+----+--------+-------+
| ID | name | Money |
+----+--------+-------+
| 1 | Li Weikang | 10000 |
+----+--------+-------+
1 row in Set
Mysql> select * from account;
+----+--------+-------+
| ID | name | Money |
+----+--------+-------+
| 1 | Li Weikang | 10000 |
| 5 | Cheng | 5000 |
+----+--------+-------+
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction of Black Horse Day12 dbutils