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)