今天持续更新人口普查系统的相关代码:
今天把所有的dao层代码发出
dbutil连接数据库代码
package dao; import java.sql.*; public class DBUtil { //eshop为数据库名称,db_user为数据库用户名db_password为数据库密码 public static String db_url = "jdbc:mysql://localhost:3306/eshop?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.util.List; import entity.School; public interface SchoolDao { public boolean add(School school); public List<School>select(); public boolean update(String classname,String classteacher,String classplace); public boolean delete(String classname); public List<School>selectclassname(String classname); }
dao层代码
package dao; import java.sql.*; import java.util.*; import entity.School; public class SchoolDaoImpl implements SchoolDao{ Connection conn=(Connection)DBUtil.getConn(); public boolean add(School school) { boolean flag=false; PreparedStatement ps=null; try { //String sql="insert into school values('"+school.getclassname()+"','"+school.getclassteacher()+"','"+school.getclassplace()+"')"; ps=conn.prepareStatement("insert into values2(classname,classteacher,classplace,classnum,zhuname,ID,sex,minzu,education)"+"values (?,?,?,?,?,?,?,?,?)"); ps.setString(1, school.getclassname()); ps.setString(2, school.getclassteacher()); ps.setString(3, school.getclassplace()); ps.setString(4, school.getclassnum()); ps.setString(5, school.getzhuname()); ps.setString(6, school.getID()); ps.setString(7, school.getsex()); ps.setString(8, school.getminzu()); ps.setString(9, school.geteducation()); ps.executeUpdate(); ps.close(); conn.close(); int i=1; if(i>0) { flag = true;System.out.println("添加成功"); } }catch(SQLException e) { e.printStackTrace(); } return flag; } public List<School> select(){ List<School> list = new ArrayList<School>(); try { String sql="select * from values2"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { School school=new School(); school.setclassname(rs.getString("classname")); school.setclassteacher(rs.getString("classteacher")); school.setclassplace(rs.getString("classplace")); school.setclassnum(rs.getString("classnum")); school.setzhuname(rs.getString("zhuname")); school.setID(rs.getString("ID")); school.setsex(rs.getString("sex")); school.setminzu(rs.getString("minzu")); school.seteducation(rs.getString("education")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<School> showdelete(String zhuname){ List<School> list = new ArrayList<School>(); try { String sql="select * from values2 where zhuname=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,zhuname); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { School school=new School(); school.setclassname(rs.getString("classname")); school.setclassteacher(rs.getString("classteacher")); school.setclassplace(rs.getString("classplace")); school.setclassnum(rs.getString("classnum")); school.setzhuname(rs.getString("zhuname")); school.setID(rs.getString("ID")); school.setsex(rs.getString("sex")); school.setminzu(rs.getString("minzu")); school.seteducation(rs.getString("education")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<School> showdelete1(String sex){ List<School> list = new ArrayList<School>(); try { String sql="select * from values2 where sex=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,sex); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { School school=new School(); school.setclassname(rs.getString("classname")); school.setclassteacher(rs.getString("classteacher")); school.setclassplace(rs.getString("classplace")); school.setclassnum(rs.getString("classnum")); school.setzhuname(rs.getString("zhuname")); school.setID(rs.getString("ID")); school.setsex(rs.getString("sex")); school.setminzu(rs.getString("minzu")); school.seteducation(rs.getString("education")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<School> showdelete2(String minzu){ List<School> list = new ArrayList<School>(); try { String sql="select * from values2 where minzu=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,minzu); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { School school=new School(); school.setclassname(rs.getString("classname")); school.setclassteacher(rs.getString("classteacher")); school.setclassplace(rs.getString("classplace")); school.setclassnum(rs.getString("classnum")); school.setzhuname(rs.getString("zhuname")); school.setID(rs.getString("ID")); school.setsex(rs.getString("sex")); school.setminzu(rs.getString("minzu")); school.seteducation(rs.getString("education")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<School> showdelete3(String education){ List<School> list = new ArrayList<School>(); try { String sql="select * from values2 where education=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,education); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { School school=new School(); school.setclassname(rs.getString("classname")); school.setclassteacher(rs.getString("classteacher")); school.setclassplace(rs.getString("classplace")); school.setclassnum(rs.getString("classnum")); school.setzhuname(rs.getString("zhuname")); school.setID(rs.getString("ID")); school.setsex(rs.getString("sex")); school.setminzu(rs.getString("minzu")); school.seteducation(rs.getString("education")); list.add(school); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public boolean delete(String zhuname) { boolean flag=false; try { String sql="delete from values2 where zhuname=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,zhuname); int i = pstmt.executeUpdate(); pstmt.close(); conn.close(); if(i>0) { flag = true; System.out.println("删除成功!!!");} }catch(SQLException e) { System.out.println("删除失败"); e.printStackTrace(); } return flag; } public boolean update(String zhuname,String ID,String sex,String minzu,String education) { boolean flag=false; try { //System.out.println(1471); PreparedStatement pstmt = conn.prepareStatement("update values2 set ID=?,sex=?,minzu=?,education=?where zhuname=?"); pstmt.setString(1,ID); pstmt.setString(2,sex); pstmt.setString(3,minzu); pstmt.setString(4,education); pstmt.setString(5,zhuname); //System.out.println(9); int i = pstmt.executeUpdate(); System.out.println(159963321); pstmt.close(); conn.close(); if(i>0) {flag = true;System.out.println("修改成功!!!");} }catch(SQLException e) { e.printStackTrace(); } return flag; } @Override public List<School> selectclassname(String classname) { // TODO 自动生成的方法存根 return null; } @Override public boolean update(String classname, String classteacher, String classplace) { // TODO 自动生成的方法存根 return false; } }