java数据库的增删改查_JAVA操作数据库的增删改查

packagecom.oracle.dao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.LinkedList;importcom.oracle.model.Emp;public classEmpDao {public int add(Connection conn,Emp emp) throwsException{

String sql="insert into emp values(?,?,?,?,?,?,?,?)";

PreparedStatement pst=conn.prepareStatement(sql);

pst.setInt(1, emp.getEmpno());

pst.setString(2, emp.getEname());

pst.setString(3, emp.getJob());

pst.setInt(4, emp.getMgr());

pst.setString(5, emp.getHiredate());

pst.setDouble(6, emp.getSal());

pst.setDouble(7,emp.getComm());

pst.setInt(8, emp.getDeptno());returnpst.executeUpdate();

}public int modify(Connection conn,Emp emp) throwsException{

String sql="Update emp set empno=?,ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=?";

PreparedStatement pst=conn.prepareStatement(sql);

pst.setInt(1, emp.getEmpno());

pst.setString(2, emp.getEname());

pst.setString(3, emp.getJob());

pst.setInt(4, emp.getMgr());

pst.setString(5, emp.getHiredate());

pst.setDouble(6, emp.getSal());

pst.setDouble(7,emp.getComm());

pst.setInt(8, emp.getDeptno());returnpst.executeUpdate();

}public int del(Connection conn,Emp emp) throwsException{

String sql="delete from emp where empno=?";

PreparedStatement pst=conn.prepareStatement(sql);

pst.setInt(1, emp.getDeptno());returnpst.executeUpdate();

}public LinkedList findAll(Connection conn) throwsException{

String sql="select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp";

PreparedStatement pst=conn.prepareStatement(sql);

ResultSet rs=pst.executeQuery();

LinkedList arr=new LinkedList();while(rs.next()) {

Emp emp= newEmp();

emp.setEmpno(rs.getInt("empno"));

emp.setEname(rs.getString("ename"));

emp.setJob(rs.getString("job"));

emp.setMgr(rs.getInt("mgr"));

emp.setHiredate(rs.getString("hiredate"));

emp.setSal(rs.getDouble("sal"));

emp.setComm(rs.getDouble("comm"));

emp.setDeptno(rs.getInt("deptno"));

arr.add(emp);

}returnarr;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值