Brief introduction:
Dbutils is fond of not liking 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 to assume that you want to delete and modify the update (String sql,object ... params)
SQL: Passed SQL statements
Params: variable number of parameters. The number of references to replace in the SQL statement
Use the Runner method to query queries using runner (String Sql,resultsethandle handle, Object ... params)
SQL: Passed SQL statements
Handle: An interface if the handle method is implemented
Params: variable parameter, the number of parameters substituted for the 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 R Esultsethandler<account> () {@Overridepublic account handle (ResultSet Rs) throws SQLException {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 ());}}
Execute result 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 |
+----+--------+-------+
Introduction of Black Horse Day12 dbutils