链接:https://blog.csdn.net/JAVA_START008/article/details/53073875?locationNum=3&fps=1
- 1、创建JDBC连接数据库类:
- public class DBConnection {
- privatestatic String url = "jdbc:oracle:thin:@localhost:1521:orcl";
- privatestatic String user = "scott";
- privatestatic String password = "tiger";
- static{
- try{
- //加载驱动类
- Class.forName("oracle.jdbc.driver.OracleDriver");
- }catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- publicstatic Connection getConn(){
- //获取连接对象
- Connectioncon = null;
- try{
- con= DriverManager.getConnection(url, user, password);
- }catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- public static voidclose(Connectionconn,PreparedStatement ps, ResultSet rs){
- if(rs!= null){
- try{
- rs.close();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(ps!= null){
- try{
- ps.close();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if(conn!= null){
- try{
- conn.close();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- <span style="font-family:monospace;white-space:pre;background-color:rgb(240,240,240);">2、创建emp 表所对应的的实体类:</span>
- public class Emp {
- private int empno;
- private String ename;
- private String job;
- private int mgr;
- private Date hiredate;
- private double sal;
- private double comm;
- private int deptno;
- public Emp(){}
- public Emp(int empno, String ename, String job, int mgr, Date hiredate,
- double sal, double comm, int deptno) {
- super();
- this.empno = empno;
- this.ename = ename;
- this.job = job;
- this.mgr = mgr;
- this.hiredate = hiredate;
- this.sal = sal;
- this.comm = comm;
- this.deptno = deptno;
- }
- @Override
- public String toString() {
- return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job
- + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal
- + ", comm=" + comm + ", deptno=" + deptno + "]";
- }
- public int getEmpno() {
- return empno;
- }
- public void setEmpno(int empno) {
- this.empno = empno;
- }
- public String getEname() {
- return ename;
- }
- public void setEname(String ename) {
- this.ename = ename;
- }
- public String getJob() {
- return job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public int getMgr() {
- return mgr;
- }
- public void setMgr(int mgr) {
- this.mgr = mgr;
- }
- public Date getHiredate() {
- return hiredate;
- }
- 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 int getDeptno() {
- return deptno;
- }
- public void setDeptno(int deptno) {
- this.deptno = deptno;
- }
- }
- <span style="font-family:Arial, Helvetica, sans-serif;"> </span><span style="font-family:Arial, Helvetica, sans-serif;">3、创建实现业务操作类,实现增删改查具体方法:</span>
- public class EmpDao {
- /**
- * 增加一条数据
- * insert into emp values(....)
- */
- public boolean addOne(Emp emp){
- boolean f = false;
- //2.定义sql语句
- String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
- //1.获取数据库连接对象
- //3.获取preparedStatement对象,以发送sql语句到数据库
- try (Connection conn = DBConnection.getConn();PreparedStatement ps = conn.prepareStatement(sql)){
- //通过ps对象给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());
- //4.执行sql语句
- int row = ps.executeUpdate();
- if(row != 0){
- f = true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return f;
- }
- /**
- * 根据ID删除
- * @param id
- * @return
- */
- public boolean delById(int id){
- boolean f = false;
- String sql = "delete from emp where empno=?";
- try(Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);) {
- ps.setInt(1, id);
- int row = ps.executeUpdate();
- if(row > 0){
- f = true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return f;
- }
- /**
- * 根据id修改
- * @return
- */
- public boolean updateById(Emp emp){
- boolean b = false;
- String s = "update emp set ename=?,job=?,mgr=?,sal=?,comm=?,deptno=?,hiredate=? where empno=?";
- try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(s);){
- ps.setString(1, emp.getEname());
- ps.setString(2, emp.getJob());
- ps.setInt(3, emp.getMgr());
- ps.setDouble(4, emp.getSal());
- ps.setDouble(5, emp.getComm());
- ps.setInt(6, emp.getDeptno());
- ps.setDate(7, emp.getHiredate());
- ps.setInt(8, emp.getEmpno());
- int row = ps.executeUpdate();
- if(row > 0){
- b = true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return b;
- }
- /**
- * 根据id查询
- * @param id
- * @return
- */
- public Emp queryById(int id){
- Emp emp = null;
- ResultSet rs = null;
- String sql = "select * from emp where empno=?";
- try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);){
- ps.setInt(1, id);
- //执行
- rs = ps.executeQuery();
- //结果集光标向下移动一行,如果查询到数据,返回true,否则返回false
- boolean b = rs.next();
- if(b){
- //查询到了数据,取出数据
- int empno = rs.getInt("EMPNO");
- String ename = rs.getString(2);
- String job = rs.getString(3);
- int mgr = rs.getInt(4);
- Date hiredate = rs.getDate(5);
- double sal = rs.getDouble(6);
- double comm = rs.getDouble(7);
- int deptno = rs.getInt(8);
- //将数据封装成对象
- // emp = new Emp();
- // emp.setEmpno(empno);
- emp = new Emp(empno,ename,job,mgr,hiredate,sal,comm,deptno);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally{
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return emp;
- }
- /**
- * 查询全部数据
- * @return
- */
- public List<Emp> queryAll(){
- List<Emp> list = new ArrayList<>();
- String sql = "select * from emp";
- try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();){
- //只要rs光标所在当前行有数据,rs.next()就返回true
- while(rs.next()){
- //取出当前行数据,封装成一个Emp对象,放入list集合
- int empno = rs.getInt("EMPNO");
- String ename = rs.getString(2);
- String job = rs.getString(3);
- int mgr = rs.getInt(4);
- Date hiredate = rs.getDate(5);
- double sal = rs.getDouble(6);
- double comm = rs.getDouble(7);
- int deptno = rs.getInt(8);
- list.add(new Emp(empno,ename,job,mgr,hiredate,sal,comm,deptno));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return list;
- }
- /**
- * 调用无返回值的存储过程
- * @throws Exception
- */
- public void callPro110() throws Exception{
- Connection conn = DBConnection.getConn();
- CallableStatement cs = conn.prepareCall("{call pro110}");
- //执行
- cs.execute();
- }
- }