package com.sdust.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;
public class CareerDaoImpl implements CareerDao {
@Override
public void insert(Career career) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
conn=DbUtils.getConnection();
String sql="insert into career(Cnum,ENO,BDATE,EDATE,COMPANY,"
+ "WORK,POSITION,PNAME,SALARY,PPOSITION,PTEL,NOTE)"
+ " values(?,?,?,?,?,?,?,?,?,?,?,?)";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, career.getcNum());
pstmt.setInt(2, career.getEno());
pstmt.setString(3,career.getbDate());
pstmt.setString(4, career.geteDate());
pstmt.setString(5, career.getCompany());
pstmt.setString(7, career.getPosition());
pstmt.setString(6, career.getWork());
pstmt.setString(8, career.getpName());
pstmt.setDouble(9, career.getSalary());
pstmt.setString(10, career.getpPosition());
pstmt.setString(11, career.getpTel());
pstmt.setString(12, career.getNote());
pstmt.executeUpdate();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public List<Career> getAllCareers(String[] params) {
// TODO Auto-generated method stub
List<Career> careers=new ArrayList<Career>();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<String> familyparam=new ArrayList<String>();//参数的集合
try {
conn=DbUtils.getConnection();
StringBuffer sql=new StringBuffer("select d.ename,d.dname,c.cnum,c.eno,c.bdate,c.edate,"
+ "c.company,c.`WORK`,c.SALARY,c.PNAME,c.POSITION,c.PTEl,c.NOTE"
+ " from dept d right join career c on d.eno=c.eno where 1=1");
if(params[0]!=null && !"".equals(params[0]))
{
sql.append(" and c.eno=?");
familyparam.add(params[0]);
}
if(params[1]!=null && !"".equals(params[1]))
{
sql.append(" and d.dname=?");
familyparam.add(params[1]);
}
if(params[2]!=null && !"".equals(params[2]))
{
sql.append(" and d.ename=?");
familyparam.add(params[2]);
}
sql.append( " order by c.eno");
//System.out.println("sql:"+sql.toString());
pstmt=conn.prepareStatement(sql.toString());
for(int i=0;i<familyparam.size();i++)
{
pstmt.setString(i+1, familyparam.get(i));
}
rs=pstmt.executeQuery();
while(rs.next())
{
Career career=new Career();
career.setcNum(rs.getInt("cNum"));
career.setEno(rs.getInt("eno"));
career.setdName(rs.getString("dName"));
career.seteName(rs.getString("eName"));
career.setbDate(rs.getString("bDate"));
career.seteDate(rs.getString("eDate"));
career.setCompany(rs.getString("company"));
career.setWork(rs.getString("work"));
career.setPosition(rs.getString("position"));
career.setSalary(rs.getDouble("salary"));
career.setpName(rs.getString("pName"));
career.setPosition(rs.getString("position"));
career.setpTel(rs.getString("pTel"));
career.setNote(rs.getString("note"));
careers.add(career);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closeResultSet(rs);
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
return careers;
}
@Override
public void delete(int cNum) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
conn=DbUtils.getConnection();
String sql="delete from career where cnum=? ";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, cNum);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public void update(Career career) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
conn=DbUtils.getConnection();
String sql="update career set eno=?,BDATE=?"
+ ",EDATE=?,COMPANY=?,WORK=?,SALARY=?,PNAME=?,PPOSITION=?,PTEL=?,NOTE=? where cnum=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, career.getEno());
pstmt.setString(2, career.getbDate());
pstmt.setString(3, career.geteDate());
pstmt.setString(4, career.getCompany());
pstmt.setString(5, career.getWork());
pstmt.setDouble(6, career.getSalary());
pstmt.setString(7, career.getpName());
pstmt.setString(8, career.getpPosition());
pstmt.setString(9, career.getpTel());
pstmt.setString(10, career.getNote());
pstmt.setInt(11, career.getcNum());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public Career getCareerByCnum(int cNum) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Career career=new Career();
System.out.println(cNum);
try {
conn=DbUtils.getConnection();
String sql="select d.ename,d.dname,c.eno,c.Cnum,"
+ "c.BDATE,c.COMPANY,c.EDATE,c.NOTE,c.PNAME,"
+ "c.POSITION,c.PPOSITION,c.PTEL,c.SALARY"
+ ",c.`WORK` from dept d right join career c on d.eno=c.eno where c.Cnum=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, cNum);
rs=pstmt.executeQuery();
if(rs.next())
{ career.setEno(rs.getInt("eno"));
career.setcNum(rs.getInt("cNum"));
career.setdName(rs.getString("dName"));
career.seteName(rs.getString("eName"));
career.setCompany(rs.getString("company"));
career.seteDate(rs.getString("eDate"));
career.setbDate(rs.getString("bDate"));
career.setNote(rs.getString("note"));
career.setpName(rs.getString("pName"));
career.setPosition(rs.getString("position"));
career.setpPosition(rs.getString("pPosition"));
career.setpTel(rs.getString("pTel"));
career.setSalary(rs.getDouble("salary"));
career.setWork(rs.getString("work"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closeResultSet(rs);
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
return career;
}
}