Using Dbutils to implement additions and deletions

Source: Internet
Author: User

Commons-dbutils is an open source JDBC Tool class library provided by Apache, which is a simple encapsulation of JDBC, very low learning costs, and the use of dbutils can greatly simplify the work of JDBC coding without compromising the performance of the program. As a result, Dbutils is the first choice for many companies that do not like hibernate.

/** * dbutils usage: Use Dbutils to implement additions and deletions change operation * @project_name DAY12 * @class_name DbUtilsDemo1 * @author Dovinya * @data 2014-8 -27 PM 11:07:09 * @version 1 * @notes *//* CREATE DATABASE day12; Use DAY12;  CREATE TABLE table1 (ID int primary key auto_increment, name varchar (), salary double); INSERT INTO table1 values (null, ' Zhang ', 234.5); INSERT INTO table1 values (null, ' Li ', 234.5); INSERT INTO table1 values (null, ' Wang ', 3242); INSERT INTO table1 values (null, ' Zhao ', 32543); INSERT INTO table1 values (null, ' Pan ', 4654);       INSERT INTO table1 values (null, ' he ', 4235); * */public class DbUtilsDemo1 {/* * use Dbutils to implement change operation */public Void Update () throws SQLException {//Get Data source object Queryrunner Runn ER = new Queryrunner (new Combopooleddatasource ()); Runner.update ("update table1 set salary = 100");} @Testpublic void Add () throws sqlexception{//gets the data source object Queryrunner Runner = new Queryrunner (new Combopooleddatasource ()); Runner.update ("INSERT INTO table1 values (1,?,?)", "Lisi", 1000);} @Testpublic void Delete () throWS sqlexception{//Gets the data source object Queryrunner Runner = new Queryrunner (new Combopooleddatasource ()); Runner.update ("Delete from table1 WHERE name = ' Lisi ');} @Testpublic void Query () throws sqlexception{//get the data source object Queryrunner Runner = new Queryrunner (new Combopooleddatasource ()) ; list<user> list = Runner.query ("Select *from table1 where id<?", New Resultsethandler<list<user>> ( ) {@Overridepublic list<user> handle (ResultSet Rs) throws SQLException {list<user> List = new arraylist< User> (); while (Rs.next ()) {User user = new User (); User.setid (Rs.getint ("id")); User.setname (rs.getstring ("name")); User.setsalary (rs.getdouble ("salary")); List.add (user); return list;}, 5); SYSTEM.OUT.PRINTLN (list); Here you can add breakpoint debugging watch}}


Using Dbutils to implement additions and deletions

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.