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();
// Emp emp = new Emp();
// emp.setEmpno(1000);
// emp.setEname(“Jimmy”);
// emp.setJob(“经理”);
// emp.setHiredate(new Date(System.currentTimeMillis()));
// emp.setSal(10000.00);
// emp.setComm(2000.00);
// emp.setDeptno(20);
// emp.setMgr(45);
//
// boolean b = dao.save(emp);
// System.out.println(b);
// EmpDao dao = new EmpDao();
// Emp emp = new Emp();
// emp.setEmpno(1000);
// emp.setEname(“Jimmy”);
// emp.setJob(“高级经理”);
// emp.setHiredate(new Date(System.currentTimeMillis()));
// emp.setSal(10000.00);
// emp.setComm(2000.00);
// emp.setDeptno(20);
// emp.setMgr(45);
//
// dao.update(emp);
// EmpDao dao = new EmpDao();
// Emp emp = new Emp();
//
// emp =dao.findById(1000);
// System.out.println(emp.getEname());
// EmpDao dao = new EmpDao();
// Emp emp = new Emp();
//
// boolean b =dao.delete(1000);
// System.out.println(b);
EmpDao dao = new EmpDao();
List empList =dao.findAll();
for(Emp e :empList){
System.out.println(e.getEname());
}
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
empList){
System.out.println(e.getEname());
}
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
[外链图片转存中…(img-ys9W6g0F-1726569624841)]
[外链图片转存中…(img-72BFazjn-1726569624842)]
最常见Java面试题解析(2021最新版)
[外链图片转存中…(img-9Zn32lde-1726569624842)]
[外链图片转存中…(img-8s6B6KwK-1726569624842)]
2021企业Java面试题精选
[外链图片转存中…(img-hJmvXUAb-1726569624842)]
[外链图片转存中…(img-FgFxYri8-1726569624843)]