DBUtils Toolkit
1. Introduction
DBUtils is an open source database tool class for Apache organization.
2. Use steps
①.Create a QueryRunner object
②. Call the update() method or query() method to execute the SQL statement
3. Construction method and static method
QueryRunner class
1. Construction method
①.Glycosideless structure
QueryRunner qr =new QueryRunner();
When using parameterless construction, you need to use an overloaded form with Connection type parameters when calling the update method and query method
②.Glycologic structure
QueryRunner qr= new QueryRunner(DataSource dataSource);
This parameter is the connection pool object
2. Static method
①.int update(Connection con,String sql,Param);
This method is used to add, delete and modify statements
Parameter description:
Parameter 1: Connection pool object (this is used when constructing without parameters)
Parameter 2: SQL statement
Parameter 3: Variable parameters (that is the value of the sql placeholder)
Return value: type int returns the number of affected rows
Simple update demo
public class Demo { public static void main(String[] args) throws Exception { /* * Demonstrate the update() method with parameter construction* * First, you have to import the jar package* Configure the configuration file of C3P0 and prepare the C3P0 tool class* Then create the QueryRunner object* Call the update method* Finally processing result*/ QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); int re = qr.update("update user set name=? where uid=?","Zhang San",2); if(re>0){ System.out.println("Modification successful"); }else { System.out.println("Modification failed"); } }}Attached with a simple C3P0 tool class
public class C3P0Utils { private static DataSource dataSource=new ComboPooledDataSource(); /** * Get DataSource implementation class object* @return */ public static DataSource getDataSource(){ return dataSource; } /** * Get connection* @return * @throws Exception */ public static Connection getConnection()throws Exception{ return dataSource.getConnection(); }}②.query(Connection con, String sql, Param...)
This method is used for query operation
Parameter description:
Parameter 1: Connection database connection object, can be used without using parameter construction
Parameter 2: SQL statement
Parameter 3: Indicates the processing method of the result set (ResultSetHandler interface)
ArrayHandler: means to store the data from the first row of the result set into an array
ArrayListHandler stores the data of each row of the result set into an array, and multiple arrays are stored into the set List<Object[]>
BeanHandler means to store the data from the first row of the result set into a Java Bean object.
BeanListHandler means that the data of each row of the result set is stored into a Java Bean object and multiple objects are stored into a collection.
ColumnListHandler means to store data from a certain column into a collection
MapHandler means to store the data from the first row of the result set into the Map set: Key: Column name value: Column value
MapListHandler means that the data of each row of the result set is stored in the Map collection and multiple Maps are stored in the List collection List<Map<,>>
ScalarHandler Gets a value: count(*) sum(price)
Parameter 4: Variable parameters (that is the value of the sql placeholder)
Use BeanListHandler to handle demo:
public void demo1() throws Exception{ QueryRunner qr = new QueryRunner(MyC3P0Utils.getDataSource()); List<Car> list = qr.query("select * from car where price<20 order by price desc", new BeanListHandler<>(Car.class)); for (Car car : list) { System.out.println(car); } }Writing javaBean class:
public class Car { private int cid; private String cname; private String company; private String grade; private double price; @Override public String toString() { return "Car [cid=" + cid + ", cname=" + cname + ", company=" + company + ", grade=" + grade + ", price=" + price + "]"; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Car(int cid, String cname, String company, String grade, double price) { super(); this.cid = cid; this.cname = cname; this.company = company; this.grade = grade; this.price = price; } public Car() { super(); // TODO Auto-generated constructor stub }}The above java study notes, DBUtils toolkit, is the full content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.