此工具類解決代碼冗餘問題。
DBUtils工具類對JDBC封裝。
核心類:QueryRunner類,提供兩個方法:
1、update() :DML操作
2、query() :DQL操作,針對返回的結果集做了再次封裝,重點要學習返回對象集合JavaBean 是java中一個可以重用的組件。本質就是一個類。
定義javabean規範:
1)類必須是public修飾
2)類中屬性必須是private
3)類中必須有無參構造
4)私有屬性必須封裝get/set方法
注意,需要將commons-dbutils-1.7.jar 添加到項目中。
添加數據的代碼:
package dbutils;
import java.sql.SQLException;
import java.util.Scanner;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 添加操作
* @author Ven
*
*/
public class Insert2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("請輸入ID:");
int eid = input.nextInt();
System.out.print("請輸入姓名:");
String ename = input.next();
System.out.print("請輸入密碼:");
String epwd = input.next();
System.out.print("請輸入年齡:");
int eage = input.nextInt();
System.out.print("請輸入生日:");
String ebirthday = input.next();
System.out.print("請輸入電話:");
String etel = input.next();
//編寫sql語句
String sql="INSERT INTO employee(eid,ename,epwd,eage,ebirthday,etel)" +
"VALUES(?,?,?,?,?,?)";
//1.創建QueryRunner類對象
QueryRunner qrunner=new QueryRunner(new ComboPooledDataSource());
try {
//2.執行sql語句
int r = qrunner.update(sql, eid,ename,epwd,eage,ebirthday,etel);
//3.處理結果
if (r>0) {
System.out.println("成功");
}else {
System.out.println("失敗");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Employee類(便於封裝重用):
package dbutils;
public class Employee {
/* 類必須是public修飾
類中屬性必須是private
類中必須有無參構造
私有屬性必須封裝get/set方法
*/
private int eid;
private String ename;
private int eage;
private String epwd;
private String ebirthday;
private String etel;
//封裝
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getEage() {
return eage;
}
public void setEage(int eage) {
this.eage = eage;
}
public String getEpwd() {
return epwd;
}
public void setEpwd(String epwd) {
this.epwd = epwd;
}
public String getEbirthday() {
return ebirthday;
}
public void setEbirthday(String ebirthday) {
this.ebirthday = ebirthday;
}
public String getEtel() {
return etel;
}
public void setEtel(String etel) {
this.etel = etel;
}
//類必須是public修飾,必須有無參構造
public Employee() {}
public Employee(int eid,String ename,String epwd,int eage,String ebirthday,String etel) {
this.eid=eid;
this.ename = ename;
this.eage = eage;
this.epwd = epwd;
this.ebirthday = ebirthday;
this.etel = etel;
}
}
package dbutils;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 查詢多條記錄操作
* @author Ven
*
*/
public class SelectMore {
public static void main(String[] args) {
String sql="SELECT * FROM employee";
QueryRunner qrunner=new QueryRunner(new ComboPooledDataSource());
try {
List<Employee> list = qrunner.query(sql, new BeanListHandler<Employee>(Employee.class));
System.out.println("ID\t姓名\t密碼\t年齡\t生日\t\t\t電話");
for (Employee employee : list) {
System.out.println(employee.getEid()+"\t"+employee.getEname()+"\t"+employee.getEpwd()+"\t"+employee.getEage()+"\t"+employee.getEbirthday()+"\t"+employee.getEtel());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}