Dbutils use (Encapsulation of JDBC)

Source: Internet
Author: User

Package Com.mysql.demo;

Import java.sql.SQLException;
Import Java.util.Iterator;
Import java.util.List;

Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Org.junit.Test;

Import Com.mysql.bean.user;

Change and delete
With Queryrunner interface and Resultsethandler
Additions and deletions: 1:queryrunner () method gets the connection to the data flow connection pool, 2: Perform the update () method provided by the Queryrunner
Query method: 1:queryrunner () gets the connection. 2: Call Queryrunner's Query () method. When handler the result set, encapsulate the data as a list collection
public class Dbutil {

Using Dbutil to complete a database crud
@Test
public void Insert () throws SQLException
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "INSERT into login values (?,?)";
Object params[]={"Keke", "Keke"};
Run.update (sql, params);
}
@Test
public void Update () throws SQLException
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "Update login set password=?" where username=? ";
Object params[]={"789", "Keke"};
Run.update (sql, params);
}

@Test
public void Delete () throws SQLException
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "Delete from login where username=?";
There's only one parameter, no need to prepare the parameter array.
Run.update (SQL, "Simant");
}
@Test
public void Find () throws SQLException
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "Select Username,password from login where username=?";
User u= (user) run.query (sql, "Keke", New Beanhandler (User.class));//process The result set into that bean, create userbean, omit this side
System.out.println (U.getpassword ());

}
@Test
public void FindAll () throws SQLException
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "SELECT * from Login";
List uu= (list) run.query (sql,new Beanlisthandler (User.class));
Multiple records are processed into a set with Beanlisthandler.
Iterator<user> It=uu.iterator ();
while (It.hasnext ())
{
User U=it.next ();
System.out.println (U.getusername ());
System.out.println (U.getpassword ());
}

}
@Test
public void Batch () throws sqlexception//batch processing if you want to insert multiple records, use a two-dimensional array
{
Get database connection Pool operations database
Queryrunner run=new Queryrunner (Sqlpooling.getdatasource ());
String sql= "INSERT into login values (?,?)";
Object param[][]=new object[3][2];//size is 3, array with size 3 is an array of size 2
for (int i=0;i<param.length;i++)
{
Param[i]=new object[]{"name" +i, "password" +i};
}
Run.batch (SQL, param);

}
}

Dbutils use (Encapsulation of JDBC)

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.