日期:2014-05-17 浏览次数:20890 次
// 更新
public boolean doUpdate(Emp emp) throws Exception {
boolean flag = false;
String sql = "update emp set ename=?,job=?,hiredate=?,salary=? where empno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, emp.getEmpno());
pstmt.setString(2, emp.getEname());
pstmt.setString(3, emp.getJob());
pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
pstmt.setFloat(5, emp.getSalary());
if (pstmt.executeUpdate() > 0) {
flag = true;
}
pstmt.close();
return flag;
}
package dao.test;
import factory.DAOFactory;
import vo.Emp;
public class TestDAOUpdate {
public static void main(String[] args) throws Exception {
Emp emp = new Emp();
emp.setEmpno(4);
emp.setEname("哈哈哈");
emp.setJob("系统管理员");
emp.setHiredate(new java.util.Date());
emp.setSalary(3000);
DAOFactory.getIEmpDAOInstance().doUpdate(emp);
}
}
@Override
public Emp findById(int empno) throws Exception {
Emp emp = null;
try {
emp = dao.findById(empno);
} catch (Exception e) {
throw e;
} finally {
dbc.close();
}
return emp;
}
@Override
public boolean doUpdate(Emp emp) throws Exception {
boolean flag = false;
try {
if (dao.findById(emp.getEmpno()) != null) {
flag = dao.doUpdate(emp);
}
} catch (Exception e) {
throw e;
}
return flag;
}