/**
*
*/
package org.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.dao.BaseDao;
import org.entity.Dept;
import org.entity.Emp;
/**
*
*
* 项目名称:test_BaseDao
* 类名称:BaseDaoUtilImpl ,继承BaseDao
* 类描述: 公共的实现类
* 创建人:Mu Xiongxiong
* 创建时间:2017-9-10 上午11:08:25
* 修改人:Mu Xiongxiong
* 修改时间:2017-9-10 上午11:08:25
* 修改备注:
* @version
*
*/
public class BaseDaoUtilImpl<T> extends BaseDao {
/**
*
* @Description: 该方法的主要作用:添加数据(Emp和Dept的)
* @Title: save
* @param @param entity 实体
* @param @param tag 区分是Emp还是Dept
* @param @return 设定文件
* @return 返回类型:int 大于0成功,否则失败
* @throws
*/
public int save(T entity,String tag) {
int rel = 0;
String sql = "";
List<Object> prams = new ArrayList<Object>();
if(tag.equals("emp")){
Emp emp = (Emp) entity; //初始化Emp对象
//添加员工
sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,to_date(?, 'YYYY-MM-DD HH24:MI:SS '),?,?,?)"; //sql语句
try {
prams.add(emp.getEmpno());
prams.add(emp.getEname());
prams.add(emp.getJob());
prams.add(emp.getMgr());
prams.add(emp.getHiredate());
prams.add(emp.getSal());
prams.add(emp.getComm());
prams.add(emp.getDeptno());
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}else if (tag.equals("dept")) {
//添加部门
Dept dept = (Dept) entity; //初始化Dept对象
sql = "insert into dept values(?,?,?)"; //sql语句
try {
prams.add(dept.getDeptno());
prams.add(dept.getDname());
prams.add(dept.getLoc());
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}
return 0; //此处执行不到,也可以写成return null;
}
/**
*
* @Description: 该方法的主要作用:修改数据
* @Title: update
* @param @param entity
* @param @return 设定文件
* @return 返回类型:int
* @throws
*/
public int update(T entity,String tag) {
int rel = 0;
String sql = "";
List<Object> prams = new ArrayList<Object>();
if(tag.equals("emp")){
Emp emp = (Emp) entity; //初始化Emp对象
//修改员工
sql = "update emp set ename = ?,job=?,mgr=?,hiredate=to_date(?, 'YYYY-MM-DD HH24:MI:SS ') ,sal=?,comm=?,deptno=? where empno=?"; //sql语句
try {
prams.add(emp.getEname());
prams.add(emp.getJob());
prams.add(emp.getMgr());
prams.add(emp.getHiredate());
prams.add(emp.getSal());
prams.add(emp.getComm());
prams.add(emp.getDeptno());
prams.add(emp.getEmpno());
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}else if (tag.equals("dept")) {
//修改部门
Dept dept = (Dept) entity; //初始化Dept对象
sql = "update dept set dname=?,loc=? where deptno =?"; //sql语句
try {
prams.add(dept.getDname());
prams.add(dept.getLoc());
prams.add(dept.getDeptno());
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}
return 0; //此处执行不到,也可以写成return null;
}
/**
*
* @Description: 该方法的主要作用:删除数据
* @Title: delete
* @param @param id
* @param @return 设定文件
* @return 返回类型:int
* @throws
*/
public int delete(int id,String tag) {
int rel = 0;
String sql = "";
List<Object> prams = new ArrayList<Object>();
if(tag.equals("emp")){
//删除员工
sql = "delete from emp where empno = ?"; //sql语句
try {
prams.add(id);
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}else if (tag.equals("dept")) {
//删除部门
sql = "delete from dept where deptno =?"; //sql语句
try {
prams.add(id);
rel = executeUpdate(sql, prams);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return rel;
}
return 0; //此处执行不到,也可以写成return null;
}
/**
*
* @Description: 该方法的主要作用:根据编号查询
* @Title: queryById
* @param @param id
* @param @return 设定文件
* @return 返回类型:T
* @throws
*/
@SuppressWarnings("unchecked")
public T queryById(int id,String tag) {
String sql = "";
List<Object> prams = new ArrayList<Object>();
if(tag.equals("emp")){
//查询员工
Emp emp = null;
sql = "select * from emp where empno = ?"; //sql语句
try {
prams.add(id);
ResultSet rs = executeQuery(sql, prams);
if(rs.next()){
emp = new Emp();
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setMgr(rs.getInt(4));
emp.setHiredate(rs.getString(5));
emp.setSal(rs.getFloat(6));
emp.setComm(rs.getFloat(7));
emp.setDeptno(rs.getInt(8));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return (T) emp;
}else if (tag.equals("dept")) {
//查询部门
sql = "select * from dept where deptno =?"; //sql语句
Dept dept = null;
try {
prams.add(id);
ResultSet rs = executeQuery(sql, prams);
if(rs.next()){
dept = new Dept();
dept.setDeptno(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setLoc(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return (T)dept;
}
return null; //此处执行不到,也可以写成return null;
}
/**
*
* @Description: 该方法的主要作用:查询全部
* @Title: queryAll
* @param @return 设定文件
* @return 返回类型:List<T>
* @throws
*/
@SuppressWarnings("unchecked")
public List<T> queryAll(String tag) {
String sql = "";
if(tag.equals("emp")){
//查询员工
sql = "select * from emp"; //sql语句
Emp emp = null;
List<Emp> empList = new ArrayList<Emp>();
try {
ResultSet rs = executeQuery(sql, null);
while(rs.next()){
emp = new Emp();
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setMgr(rs.getInt(4));
emp.setHiredate(rs.getString(5));
emp.setSal(rs.getFloat(6));
emp.setComm(rs.getFloat(7));
emp.setDeptno(rs.getInt(8));
empList.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return (List<T>) empList;
}else if (tag.equals("dept")) {
//添加部门
sql = "select * from dept"; //sql语句
Dept dept = null;
List<Dept> deptList = new ArrayList<Dept>();
try {
ResultSet rs = executeQuery(sql, null);
while(rs.next()){
dept = new Dept();
dept.setDeptno(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setLoc(rs.getString(3));
deptList.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeConnnection(); //关闭连接
}
return (List<T>)deptList;
}
return null; //此处执行不到,也可以写成return null;
}
}