package com.clc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Dao层的父类,设置此类为抽象类,自身不能实例化,供dao层的其他类继承
* @author Administrator
*
*/
public abstract class BaseDao {
protected Connection conn = null;
protected PreparedStatement pstmt = null;
protected ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/java_mysql";
String user = "root";
String password = "root";
/**
* 获取连接对象
* @throws Exception
*/
public void getConn() throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
}
/**
* 增删改的通用方法
* @param sql
* @param params 可变参数,是数组的一种特殊用法,在参数sql后面可以接上一个或多个对应类型的参数
*/
public void edit(String sql,Object... params) {
try {
getConn();
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
int row = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeAll();
}
}
/**
* 关闭所有资源
*/
public void closeAll() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.clc.dao;
import java.util.ArrayList;
import java.util.List;
import com.zhiyou100.clc.bean.Emp;
/**
* 表emp的操作类
*
* @author Administrator
*
*/
public class EmpDao extends BaseDao {
/**
* 添加员工
*
* @param ename 员工姓名
* @param job 工作
* @param sal 工资
*/
public void addEmp(String ename, String job, double sal) {
String sql = "insert emp values(null,?,?,?,now())";
edit(sql, ename, job, sal);
}
/**
* 根据员工号删除员工信息
*
* @param empId
*/
public void deleteById(int empId) {
String sql = "delete from emp where empId = ?";
edit(sql, empId);
}
/**
* 根据员工号修改员工信息
*
* @param empId 员工号
* @param ename 姓名
* @param job 工作
* @param sal 工资
*/
public void updateById(int empId, String ename, String job, double sal) {
String sql = "update emp set ename = ?,job = ?,sal = ? where empId = ?";
edit(sql, ename, job, sal, empId);
}
/**
* 根据员工号查询员工信息
*
* @param empId
*/
public Emp selectById(int empId) {
Emp emp = null;
try {
getConn();
String sql = "select empId,ename,job,sal,hiredate from emp where empId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, empId);
rs = pstmt.executeQuery();
while (rs.next()) {
emp = new Emp(rs.getInt("empId"), rs.getString("ename"), rs.getString("job"), rs.getDouble("sal"),
rs.getDate("hiredate"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeAll();
}
return emp;
}
/**
* 查询所有员工信息
*/
public List<Emp> selectAll() {
List<Emp> empList = new ArrayList<Emp>();
try {
getConn();
String sql = "select empId,ename,job,sal,hiredate from emp";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Emp emp = new Emp(rs.getInt("empId"), rs.getString("ename"), rs.getString("job"), rs.getDouble("sal"),
rs.getDate("hiredate"));
empList.add(emp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeAll();
}
return empList;
}
}