最后
如果觉得本文对你有帮助的话,不妨给我点个赞,关注一下吧!
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
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 getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
}
2.编写数据库操作类EmpDao:【封装CURD】
public class EmpDao {
//查询所有
public List findAll(){
Connection conn = null;
List empList = new ArrayList();
try {
conn = DBUtil.getConnection();
String sql = "select * from emp ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
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.setHiredate(rs.getDate(“hiredate”));
e.setSal(rs.getDouble(“sal”));
e.setComm(rs.getDouble(“comm”));
e.setDeptno(rs.getInt(“deptno”));
empList.add(e);
}
return empList;
} catch (SQLException e) {
e.printStackTrace();
}
return empList;
}
public Emp findById(int id){
Connection conn =null;
try {
conn = DBUtil.getConnection();
String sql = "select * from emp "
+“where empno =?”;
PreparedStatement ps = conn.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.setHiredate(rs.getDate(“hiredate”));
e.setSal(rs.getDouble(“sal”));
e.setComm(rs.getDouble(“comm”));
e.setDeptno(rs.getInt(“deptno”));
return e;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“根据ID查询员工失败!”,e);
}finally{
DBUtil.close(conn);
}
return null;
}
public boolean save(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = “insert into emp values(”
+“?,?,?,?,?,?,?,?)”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getEmpno());
ps.setString(2, emp.getEname());
ps.setString(3, emp.getJob());
ps.setInt(4, emp.getMgr());
ps.setDate(5, emp.getHiredate());
ps.setDouble(6,emp.getSal());
ps.setDouble(7, emp.getComm());
ps.setInt(8, emp.getDeptno());
int count = ps.executeUpdate();
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“保存员工失败!”,e);
}finally{
DBUtil.close(conn);
}
}
public void update(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "update emp set "
+“ename =?,”
+“job =?,”
+“mgr =?,”
+“hiredate =?,”
+“sal =?,”
+“comm =?,”
+"deptno =? "
+“where empno=?”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, emp.getEname());
ps.setString(2, emp.getJob());
ps.setInt(3, emp.getMgr());
ps.setDate(4, emp.getHiredate());
ps.setDouble(5,emp.getSal());
ps.setDouble(6, emp.getComm());
ps.setInt(7, emp.getDeptno());
ps.setInt(8, emp.getEmpno());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“修改员工失败!”,e);
}finally{
DBUtil.close(conn);
}
}
public boolean delete(int id){
Connection conn =null;
try {
conn = DBUtil.getConnection();
String sql = "delete from emp "
+“where empno =?”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
int count = ps.executeUpdate();
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“根据ID删除员工失败!”,e);
}finally{
DBUtil.close(conn);
}
}
}
3.编写EmpDao测试类
public class EmpDaoTest {
public static void main(String[] args) {
// EmpDao dao = new EmpDao();
写在最后
可能有人会问我为什么愿意去花时间帮助大家实现求职梦想,因为我一直坚信时间是可以复制的。我牺牲了自己的大概十个小时写了这片文章,换来的是成千上万的求职者节约几天甚至几周时间浪费在无用的资源上。
上面的这些(算法与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源
大概十个小时写了这片文章,换来的是成千上万的求职者节约几天甚至几周时间浪费在无用的资源上。
[外链图片转存中…(img-TZVy38Yi-1715637111574)]
[外链图片转存中…(img-3vqFU7ZR-1715637111574)]
上面的这些(算法与数据结构)+(Java多线程学习手册)+(计算机网络顶级教程)等学习资源