封装加载驱动、建立连接、关闭连接的方法的类DBUtil;与数据库表Emp相对应的实体类;封装了对表Emp进行增删改查方法的类Dao;测试类Dao中方法的Test类
---------------------------------------------------------------------------------
package util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 封装了加载驱动的静态块,与数据库建立连接和关闭连接的静态方法
* 用来管理数据库的连接数据库的链接信息保存在属性文件中
* @author Administrator
*
*/
public class DBUtil {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
System.out.println(conn.getClass().getName());
close(conn);
}
private static BasicDataSource ds;
static {
try {
//加载属性文件数据
Properties prop = new Properties();
prop.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
String driverclass = prop.getProperty("jdbc.driverclass");
String url = prop.getProperty("jdbc.url");
String user = prop.getProperty("jdbc.user");
String password = prop.getProperty("jdbc.password");
String strMaxActive = prop.getProperty("dbcp.MaxActive");
String strInitialSize = prop.getProperty("dbcp.InitialSize");
//1、加载驱动
//实例化并初始化连接池。
ds = new BasicDataSource();
ds.setDriverClassName(driverclass);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
ds.setMaxActive(Integer.parseInt(strMaxActive));
ds.setInitialSize(Integer.parseInt(strInitialSize));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取属性文件异常!",e);
}
}
//2、创建连接
/*
* 定义一个方法需要什么?
* 1、返回值类型:
* 是否有运算结果?如果有,结果的类型就是返回值类型。
* 2、参数列表:
* 方法中是否有不确定的参数参与运算?如果有,即为参数。
*/
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
//3、归还连接
public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("归还连接异常!",e);
}
}
}
}
属性文件:db.properties
jdbc.driverclass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost :1521:xe
jdbc.user=system
jdbc.password=zjz1992
#dbcp param
dbcp.MaxActive=1
dbcp.InitialSize=1
----------------------------------------------------------------------------------------
JavaBean
满足如下规范的类:
- 有package
- 有无参构造器
- 实现序列化接口
- 有get/set方法
实体类:
package entity;
import java.io.Serializable;
import java.sql.Date;
/**
* 1.通常实体类名和表名一致
* 2.通常该类中的属性名和字段名一致
* 3.通常该类中的属性都使用封装类型(封装类型可以为null)
* @author Administrator
*
*/
public class Emp implements Serializable {
private static final long serialVersionUID = 1L;
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hirdate;
private Double sal;
private Double comm;
private Integer depton;
public Emp(){
}
public Emp(Integer empno, String ename, String job, Integer mgr, Date hirdate, Double sal, Double comm,Integer depton) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hirdate = hirdate;
this.sal = sal;
this.comm = comm;
this.depton = depton;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHirdate() {
return hirdate;
}
public void setHirdate(Date hirdate) {
this.hirdate = hirdate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDepton() {
return depton;
}
public void setDepton(Integer depton) {
this.depton = depton;
}
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import entity.Emp;
import util.DBUtil;
public class EmpDao {
/**
* 查询所有员工
* @return List<Emp>
*/
public List<Emp> findAll(){
Connection con = null;
try {
con = DBUtil.getConnection();
String sql = "SELECT * FROM emp ";
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Emp> emps = new ArrayList<Emp>();
while(rs.next()){
Emp e = new Emp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setHirdate(rs.getDate("hirdate"));
e.setSal(rs.getDouble("sal"));
e.setComm(rs.getDouble("comm"));
e.setDepton(rs.getInt("depton"));
emps.add(e);
}
return emps;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询员工失败!",e);
}finally{
DBUtil.close(con);
}
}
/**
* 根据Id查询员工
* @return Emp
*/
public Emp findById(int id){
Connection con = null;
try {
con = DBUtil.getConnection();
String sql = "SELECT * FROM emp "
+ "WHERE empno=?";
PreparedStatement ps= con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
Emp e = new Emp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setHirdate(rs.getDate("hirdate"));
e.setSal(rs.getDouble("sal"));
e.setComm(rs.getDouble("comm"));
e.setDepton(rs.getInt("depton"));
return e;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("根据Id查询员工失败!",e);
}finally{
DBUtil.close(con);
}
return null;
}
/**
* 修改员工
*/
public void save(Emp emp){
Connection con = null;
try {
con = DBUtil.getConnection();
String sql =
"update emp set "
+ "ename=?,"
+ "job=?,"
+ "mgr=?,"
+ "hirdate=?,"
+ "sal=?,"
+ "comm=?,"
+ "depton=? "
+ "where empno=?";
PreparedStatement ps =
con.prepareStatement(sql);
ps.setString(1, emp.getEname());
ps.setString(2, emp.getJob());
ps.setInt(3, emp.getMgr());
ps.setDate(4, emp.getHirdate());
ps.setDouble(5, emp.getSal());
ps.setDouble(6, emp.getComm());
ps.setInt(7, emp.getDepton());
ps.setInt(8, emp.getEmpno());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("修改员工失败!",e);
}finally{
DBUtil.close(con);
}
}
/**
* 根据Id删除员工
*/
public void delete(int id){
Connection con = null;
try {
con = DBUtil.getConnection();
String sql = "DELETE FROM emp "
+ "WHERE empno=?";
PreparedStatement ps= con.prepareStatement(sql);
ps.setInt(1, id);
int i = ps.executeUpdate();
if(i>0){
System.out.println("删除员工成功!");
}else{
System.out.println("没有此人!");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("根据Id删除员工失败!",e);
}finally{
DBUtil.close(con);
}
}
}
package test;
/**
*测试EmpDao中的方法
*/
public class Test {
/**
*测试EmpDao的findAll方法
*/
@Test
public void testfindAll(){
EmpDao dao = new EmpDao();
List<Emp> emps = dao.findAll();
for(Emp e:emps){
System.out.println(e.getEmpno()+","+e.getEname());
}
}
/**
*测试EmpDao的findById方法
*/
@Test
public void testfindById(){
EmpDao dao = new EmpDao();
Emp e = dao.findById(1);
if(e!=null){
System.out.println(e.getEname()+","+e.getJob());
}else{
System.out.println("没有此人!");
}
}
/**
* 测试EmpDao的save方法
*/
@Test
public void testsave(){
EmpDao dao = new EmpDao();
Emp e = new Emp(2,"小花","咬人",13,null,5000.0,250.0,45);
dao.save(e);
System.out.println(e.getEname()+","+e.getJob());
}
/**
* 测试EmpDao的delete方法
*/
@Test
public void testdelete(){
//假设要查询的员工的id为:
int id = 1;
EmpDao dao = new EmpDao();
dao.delete(id);
}
}
--------------------------------------------------------------------------------------------