Introduction of Black Horse Day12 dbutils

Source: Internet
Author: User

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

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.