Introduction of Black Horse Day12 dbutils

Source: Internet
Author: User

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

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.