此工具类解决代码冗余问题。
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();
}
}
}