12.5
有一说一,今天几乎没有学习。今天有事去龙山办活动在那边待了一天,回来也差不多到现在了。所以写一写dao层的代码吧!
员工培训系统。。dao层
链接数据库代码
package dao; import java.sql.*; public class DBUtil { //eshop为数据库名称,db_user为数据库用户名db_password为数据库密码 public static String db_url = "jdbc:mysql://localhost:3306/staffmanage?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"; public static String db_user = "root"; public static String db_password = "root1"; public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_password); System.out.println("连接成功"); } catch (Exception e) { System.out.println("连接失败"); e.printStackTrace(); } return conn; } public static void close(Statement state, Connection conn) { if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs, Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import entity.Staff; public class StaffDaoImpl implements StaffDao{ Connection conn=(Connection)DBUtil.getConn(); public String teachershowpass(String iD) { // TODO 自动生成的方法存根 String password=null; try { String sql="select * from teacher where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,iD); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { Staff staff=new Staff(); staff.setTeapassword(rs.getString("teapassword")); password=staff.getTeapassword(); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return password; } public String stushowpass(String iD) { // TODO 自动生成的方法存根 String password=null; try { String sql="select * from student where stuID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,iD); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { Staff staff=new Staff(); staff.setStupassword(rs.getString("stupassword")); password=staff.getStupassword(); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return password; } public List<Staff> stushow(String iD) { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from student where stuID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,iD); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setStusex(rs.getString("stusex")); school.setStuclass(rs.getString("stuclass")); school.setStutitle(rs.getString("stutitle")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean stuupdate(String stuID, String stuname, String stusex, String stuclass, String stutitle) { // TODO 自动生成的方法存根 boolean flag=false; try { String sql="update student set stuname=?,stusex=?,stuclass=?,stutitle=?where stuID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(5, stuID); pstmt.setString(1,stuname); System.out.println(stuname+'2'); pstmt.setString(2,stusex); pstmt.setString(3,stuclass); pstmt.setString(4,stutitle); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } public List<Staff> stushowgrade(String iD) { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from grade where stuID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,iD); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setCourse(rs.getString("course")); school.setGrade(rs.getString("grade")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<Staff> teashow(String teaID) { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from teacher where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,teaID); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setTeaID(rs.getString("teaID")); school.setTeaname(rs.getString("teaname")); school.setTeasex(rs.getString("teasex")); school.setTeaclass(rs.getString("teaclass")); school.setTeatitle(rs.getString("teatitle")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean teaupdate(String teaID, String teaname, String teasex, String teaclass, String teatitle) { // TODO 自动生成的方法存根 boolean flag=false; try { String sql="update student set teaname=?,teasex=?,teaclass=?,teatitle=?where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(5, teaID); pstmt.setString(1,teaname); System.out.println(teaname+'2'); pstmt.setString(2,teasex); pstmt.setString(3,teaclass); pstmt.setString(4,teatitle); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } public boolean gradeadd(String stuID, String stuname, String course, String grade,String teaID) { // TODO 自动生成的方法存根 boolean flag=false; PreparedStatement ps=null; try { System.out.println("jinru"); ps=conn.prepareStatement("insert into grade(stuID,stuname,course,grade,teaID)"+"values(?,?,?,?,?)"); ps.setString(1,stuID); ps.setString(2,stuname); ps.setString(3,course); ps.setString(4,grade); ps.setString(5, teaID); System.out.println("jinru123"); int i=ps.executeUpdate(); ps.close(); conn.close(); if(i>0) { flag = true;System.out.println("添加成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public List<Staff> gradeshow(String teaID) { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from grade where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,teaID); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setCourse(rs.getString("course")); school.setGrade(rs.getString("grade")); school.setTeaID(rs.getString("teaID")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean updategrade(String stuID, String grade) { // TODO 自动生成的方法存根 boolean flag=false; try { String sql="update grade set grade=?where stuID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,grade); pstmt.setString(2,stuID); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } public List<Staff> showpass() { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from grade WHERE grade >='60'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setCourse(rs.getString("course")); school.setGrade(rs.getString("grade")); school.setTeaID(rs.getString("teaID")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<Staff> showexcellent() { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from grade WHERE grade >='90'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setCourse(rs.getString("course")); school.setGrade(rs.getString("grade")); school.setTeaID(rs.getString("teaID")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<Staff> allstushow() { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from student"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setStusex(rs.getString("stusex")); school.setStuclass(rs.getString("stuclass")); school.setStutitle(rs.getString("stutitle")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<Staff> allteashow() { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from teacher"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setTeaID(rs.getString("teaID")); school.setTeaname(rs.getString("teaname")); school.setTeasex(rs.getString("teasex")); school.setTeaclass(rs.getString("teaclass")); school.setTeatitle(rs.getString("teatitle")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean addtea(String iD, String name, String sex, String class1, String title) { // TODO 自动生成的方法存根 boolean flag=false; PreparedStatement ps=null; try { System.out.println("jinru"); ps=conn.prepareStatement("insert into teacher(teaID,teaname,teasex,teaclass,teatitle)"+"values(?,?,?,?,?)"); ps.setString(1,iD); ps.setString(2,name); ps.setString(3,sex); ps.setString(4,class1); ps.setString(5, title); System.out.println("jinru123"); int i=ps.executeUpdate(); ps.close(); conn.close(); if(i>0) { flag = true;System.out.println("添加成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public boolean addstu(String iD, String name, String sex, String class1, String title) { // TODO 自动生成的方法存根 boolean flag=false; PreparedStatement ps=null; try { System.out.println("jinru"); ps=conn.prepareStatement("insert into student(stuID,stuname,stusex,stuclass,stutitle)"+"values(?,?,?,?,?)"); ps.setString(1,iD); ps.setString(2,name); ps.setString(3,sex); ps.setString(4,class1); ps.setString(5, title); System.out.println("jinru123"); int i=ps.executeUpdate(); ps.close(); conn.close(); if(i>0) { flag = true;System.out.println("添加成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public List<Staff> shownopass() { // TODO 自动生成的方法存根 List<Staff> list = new ArrayList<Staff>(); try { System.out.println(1); String sql="select * from grade WHERE grade <'60'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println(2); while(rs.next()) { Staff school=new Staff(); school.setStuID(rs.getString("stuID")); school.setStuname(rs.getString("stuname")); school.setCourse(rs.getString("course")); school.setGrade(rs.getString("grade")); school.setTeaID(rs.getString("teaID")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean deletetea(String iD) { // TODO 自动生成的方法存根 boolean flag=false; PreparedStatement ps=null; try { System.out.println("jinru"); ps=conn.prepareStatement("delete from teacher where teaID=?"); ps.setString(1,iD); System.out.println("jinru123"); int i=ps.executeUpdate(); ps.close(); conn.close(); if(i>0) { flag = true;System.out.println("删除成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public boolean deletestu(String iD) { // TODO 自动生成的方法存根 boolean flag=false; PreparedStatement ps=null; try { System.out.println("jinru"); ps=conn.prepareStatement("delete from student where stuID=?"); ps.setString(1,iD); System.out.println("jinru123"); int i=ps.executeUpdate(); ps.close(); conn.close(); if(i>0) { flag = true;System.out.println("删除成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public boolean updatetea(String iD1, String name, String sex, String class1, String title) { // TODO 自动生成的方法存根 boolean flag=false; try { String sql="update teacher set teaname=?,teasex=?,teaclass=?,teatitle=?where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(5, iD1); pstmt.setString(1,name); pstmt.setString(2,sex); pstmt.setString(3,class1); pstmt.setString(4,title); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } public boolean updatestu(String iD1, String name, String sex, String class1, String title) { // TODO 自动生成的方法存根 boolean flag=false; try { String sql="update student set teaname=?,teasex=?,teaclass=?,teatitle=?where teaID=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(5, iD1); pstmt.setString(1,name); pstmt.setString(2,sex); pstmt.setString(3,class1); pstmt.setString(4,title); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } }
数据层代码
package entity; public class Staff { private String stuID; private String stupassword; private String stuname; private String stusex; private String stuclass; private String stutitle; private String course; private String grade; public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } private String teaID; private String teapassword; private String teaname; private String teasex; private String teaclass; private String teatitle; public String getTeaID() { return teaID; } public void setTeaID(String teaID) { this.teaID = teaID; } public String getTeapassword() { return teapassword; } public void setTeapassword(String teapassword) { this.teapassword = teapassword; } public String getTeaname() { return teaname; } public void setTeaname(String teaname) { this.teaname = teaname; } public String getTeasex() { return teasex; } public void setTeasex(String teasex) { this.teasex = teasex; } public String getTeaclass() { return teaclass; } public void setTeaclass(String teaclass) { this.teaclass = teaclass; } public String getTeatitle() { return teatitle; } public void setTeatitle(String teatitle) { this.teatitle = teatitle; } public String getStuclass() { return stuclass; } public void setStuclass(String stuclass) { this.stuclass = stuclass; } public String getStutitle() { return stutitle; } public void setStutitle(String stutitle) { this.stutitle = stutitle; } public String getStuID() { return stuID; } public void setStuID(String stuID) { this.stuID = stuID; } public String getStupassword() { return stupassword; } public void setStupassword(String stupassword) { this.stupassword = stupassword; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public String getStusex() { return stusex; } public void setStusex(String stusex) { this.stusex = stusex; } }