




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
studentDao.javapackage DB.Dao;public interface studentDao /保存学生void saveStudent(student stu)throws SQLException;/通过学号删除学生void deleteStudent(String stuId)throws SQLException;/通过学生的学号修改指定学生的信息void updateStudent(student stu)throws SQLException;/查找学生,将满足condition条件的学生放在集合中;如果condition为null或表示查找所有学生/condition的内容是SQL查询的条件部分如: 属性=值/以分页形式显示,pageSize表示每页几条记录,currentPage表示当前第几条记录/如果pageSize=0表示不分页List findStudents(String condition,int pageSize,int currentPage)throws SQLException;/关闭所有资源void close();/得到学生表中的班级List getAllClass()throws SQLException;userDao.javapackage DB.Dao;public interface userDao /查找指定的用户名,和密码的用户信息user findUser(user user)throws SQLException;/关闭所有资源void close();studentDaoImpl.javapackage DB.Dao.Impl;public class studentDaoImpl extends DBtemplate implements studentDao public studentDaoImpl(Connection conn) throws SQLException super(conn);public void close() this.CloseAll();public void deleteStudent(String stuId) throws SQLException String sql=delete 学生表;if(!(stuId=null|.equals(stuId)sql=sql+ where 学号=+stuId.trim()+;this.executeUpdate(sql);/pageSize=0表示不分页public List findStudents(String condition,int pageSize,int currentPage) throws SQLException List students=new ArrayList();Statement stm1=null;ResultSet rs1=null;try stm1= conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);String sql=select 学号,姓名,年龄 from 学生表;if(!(condition=null|.equals(condition)sql=sql+ where +condition;sql=sql+ order by 学号 asc;rs1=stm1.executeQuery(sql); if(currentPage=0) currentPage=1; if(pageSize=0)student stu=new student();stu.setAge(rs1.getInt(年龄);stu.setName(rs1.getString(姓名);stu.setStuId(rs1.getString(学号);students.add(stu);count=count-pageSize;catch(SQLException E)throw E;finallyif(rs1!=null)rs1.close();if(stm1!=null)stm1.close();return students;public void saveStudent(student stu) throws SQLException String sql=insert into 学生表(学号,姓名,年龄) values(?,?,?);preparedStatementDeal(sql,stu);private void preparedStatementDeal(String sql,student stu)throws SQLExceptionPreparedStatement pre=conn.prepareStatement(sql);pre.setString(1, stu.getStuId();pre.setString(2, stu.getName();pre.setInt(3, stu.getAge();pre.executeUpdate();if(pre!=null)pre.close();public void updateStudent(student stu) throws SQLException String sql=update 学生表 set 学号=?,姓名=?,年龄=? where 学号=+stu.getStuId()+;preparedStatementDeal(sql,stu);public List getAllClass()throws SQLException List classs=new ArrayList(); String sql=select distinct(班级) from(select left(学号,5) as 班级 from 学生表) a; rs=executeQuery(sql); while(rs.next() classs.add(rs.getString(班级); return classs;userDaoImplpackage DB.Dao.Impl;public class userDaoImpl extends DBtemplate implements userDaopublic userDaoImpl(Connection conn) throws SQLException super(conn);/ TODO Auto-generated constructor stubpublic void close() this.CloseAll();public user findUser(user user) throws SQLException user tempUser=null;String sql=select * from 用户表 where 用户名=? and 密码=?;PreparedStatement pre=conn.prepareStatement(sql);pre.setString(1,user.getName();pre.setString(2, user.getPassWord();rs=pre.executeQuery();if(rs.next()tempUser=new user();tempUser.setPassWord(rs.getString(密码);tempUser.setName(rs.getString(用户名);if(pre!=null)pre.close();return tempUser;studentServicesDaopackage DB.ServicesDao;public interface studentServicesDao /保存学生,要求在数据库中不能有同学号的学生void saveStudent(student stu)throws myException;/删除指定学号的学生void deleteStudent(String stuId)throws myException;/以学生学号为条件,更新学生信息void updateStudent(student stu)throws myException;/将学生信息进行分页查询,PageSize表示一页多少数据,page表示当前显示第几页/className为班级名,如果className为null或表示所有学生/如果pageSize=-1,表示非分页显示List getStudents(int pageSize,int page,String className)throws myException;/查找指定学号的学生student findByStuId(String stuId)throws myException;/释放资源void Close();/得到班级List getAllClass()throws myException;userServicesDaopackage DB.ServicesDao;public interface userServicesDao /查找指定用户的信息user findUser(user user)throws myException;/释放资源void Close();studentServicesDaoImplpackage DB.ServicesDao.Impl;public class studentServicesDaoImpl implements studentServicesDao Connection conn=null;studentDao dao=null; public studentServicesDaoImpl() try conn=connectionFactory.getConnection();dao=new studentDaoImpl(conn); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); public void Close() if(dao!=null)dao.close();public void deleteStudent(String stuId) throws myException if(stuId=null|.equals(stuId)throw new myException(要删除的学号为空);try dao.deleteStudent(stuId);mit(); catch (SQLException e) e.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();public student findByStuId(String stuId) throws myException student stu=null; checkString(stuId, 学号); try List students=dao.findStudents(学号=+stuId+, 0,0); if(students!=null&students.size()=1) stu=students.get(0); mit(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();return stu;private void checkString(String str,String obj)throws myExceptionif(str=null|.equals(str)throw new myException(用户输入的+obj+为空);public List getAllClass() throws myException List classes=null;try classes=dao.getAllClass();mit(); catch (SQLException e) e.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();return classes;public List getStudents(int pageSize, int page, String className)throws myException List students=null;String condition=;if(!(className=null|.equals(className)condition=学号 like +className+%;try students=dao.findStudents(condition, pageSize, page); mit(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();return students;private void checkStudent(student stu)throws myExceptionif(stu=null)throw new myException(用户输入的学生对象为空);checkString(stu.getStuId(),学号);checkString(stu.getName(),姓名);if(stu.getAge()=0)throw new myException(用户输入的学生年龄为+stu.getAge()+不合法);public void saveStudent(student stu) throws myException checkStudent(stu);try student stuTemp=this.findByStuId(stu.getStuId();if(stuTemp=null)dao.saveStudent(stu);mit();elsethrow new myException(stu.getStuId()+的学号已经存在); catch (Exception e) e.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();public void updateStudent(student stu) throws myException checkStudent(stu);try student stuTemp=this.findByStuId(stu.getStuId();if(stuTemp!=null)dao.updateStudent(stu);mit();elsethrow new myException(stu.getStuId()+的学生不存在); catch (SQLException e) e.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();userServicesDaoImplpackage DB.ServicesDao.Impl;public class userServicesDaoImpl implements userServicesDao Connection conn; userDao dao=null; public userServicesDaoImpl() conn=connectionFactory.getConnection(); try dao=new userDaoImpl(conn); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public void Close() if(dao!=null)dao.close();public user findUser(user user) throws myException user userTemp=null;if(user=null)throw new myException(用户对象为空);if(user.getName()=null|.equals(user.getName()throw new myException(用户名为空);if(user.getPassWord()=null|.equals(user.getPassWord()throw new myException(用户密码为空);try userTemp=dao.findUser(user);mit(); catch (SQLException e) e.printStackTrace();try conn.rollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();throw new myException(e.getMessage();return userTemp;connectionFactorypackage DB.Util;public class connectionFactory private connectionFactory() private static String driver; private static String url; private static String name; private static String pwd; public static void setDriver(String driver) connectionFactory.driver = driver; try loadDriver(); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); public static void setUrl(String url) connectionFactory.url = url;public static void setName(String name) connectionF = name;public static void setPwd(String pwd) connectionFactory.pwd = pwd;public static void loadDriver() throws ClassNotFoundExceptiontry Class.forName(driver); catch (ClassNotFoundException e) e.printStackTrace();throw e;public static Connection getConnection() Connection conn=null; try conn=DriverManager.getConnection(url,name,pwd);conn.setAutoCommit(false); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); return conn; connectionFactoryProppackage DB.Util;public class connectionFactoryProp private connectionFactoryProp() private static String driver; private static String url; private static String name; private static String pwd; private static Properties pro=new Properties(); static InputStream is=connectionFactoryProp.class.getResourceAsStream(perties); try pro.load(is);driver=pro.getProperty(driver);url=pro.getProperty(url);name=pro.getProperty(user);pwd=pro.getProperty(password);Class.forName(driver); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); public static Connection getConnection() Connection conn=null; try conn=DriverManager.getConnection(url,name,pwd);conn.setAutoCommit(false); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); return conn; dbConnectionpackage DB.Util;public class dbConnection extends HttpServlet Overridepublic void init(ServletConfig config) throws ServletException super.init(config);String driver=config.getInitParameter(driver);String url=config.getInitParameter(url);String user=config.getInitParameter(user);String pwd=config.getInitParameter(password);connectionFactory.setDriver(driver);connectionFactory.setName(user);connectionFactory.setPwd(pwd);connectionFactory.setUrl(url);try connectionFactory.loadDriver(); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();throw new ServletException(e.getMessage();System.out.println(connectionFactory初始化完毕);DBtemplatepackage DB.Util;public class DBtemplate protected Connection conn;protected Statement stm;protected ResultSet rs;protected DBtemplate(Connection conn) throws SQLExceptionthis.conn=conn;stm=conn.createStatement();protected ResultSet executeQuery(String sql)throws SQLExceptionreturn stm.executeQuery(sql);protected int executeUpdate(String sql) throws SQLExceptionreturn stm.executeUpdate(sql);protected PreparedStatement createPreparedStatement(String sql) throws SQLExceptionreturn conn.prepareStatement(sql);public void CloseAll()try if(rs!=null)rs.close(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();try if(stm!=null)stm.close(); catch (SQLException e) System.out.println(Statment);try if(conn!=null)conn.close(); catch (SQLException e) System.out.println(Connection);myExceptionpackage Exception;public class myException extends Exception public myException(String message) super(message); public myException(Exception e) super(e); accesspackage filter;public class access implements Filter public void destroy() / TODO Auto-generated method stubpublic void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException HttpServletRequest request=(HttpServletRequest) req;HttpServletResponse response=(HttpServletResponse) res;HttpSession session=request.getSession();if(session.getAttribute(enter)=null)/request.getRequestDispatcher(/index.jsp).forward(request, response);System.out.println(request.getServletPath();response.sendRedirect(request.getContextPath()+/index.jsp);else chain.doFilter(request,response);public void init(FilterConfig arg0) throws ServletException / TODO Auto-generated method stubencoderpackage filter;public class encoder implements Filter public void destroy() / TODO Auto-generated method stubpublic void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException HttpServletRequest request=(HttpServletRequest) req;HttpServletResponse response=(HttpServletResponse) res;request.setCharacterEncoding(UTF-8);response.setCharacterEncoding(UTF-8);chain.doFilter(request,response);public void init(FilterConfig arg0) throws ServletException / TODO Auto-generated method stubapplicationListenerpackage listener;public class applicationListener implements ServletContextListenerpublic void contextDestroyed(ServletContextEvent arg0) / TODO Auto-generated method stubpublic void contextInitialized(ServletContextEvent event) ServletContext application=event.getServletContext();String driver=application.getInitParameter(driver);String url=application.getInitParameter(url);String user=application.getInitParameter(user);String pwd=application.getInitParameter(password);connectionFactory.setDriver(driver);connectionFactory.setName(user);connectionFactory.setPwd(pwd);connectionFactory.setUrl(url);studentServicesDao dao=null;trydao=new studentServicesDaoImpl();List classes=dao.getAllClass();if(classes!=null&classes.size()=1)application.setAttribute(classes,classes);catch(myException e)finallyif(dao!=null)dao.Close();studentpackage pojo;public class student private String name;private int age;private String stuId;public String getName() return name;public void setName(String name) = name;public int getAge() return age;public void setAge(int age) this.age = age;public String getStuId() return stuId;public void setStuId(String stuId) this.stuId = stuId;userpackage pojo;public class user private String name;private String passWord;public String getName() return name;public void setName(String name) = name;public String getPassWord() return passWord;public void setPassWord(String passWord) this.passWord = passWord;deleteStudentpackage servlet.student;public class deleteStudent extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doPost(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException dispClassStudentpackage servlet.student;public class dispClassStudent extends HttpServlet private int pageSize=5;public void init(ServletConfig config) throws ServletException String pazeS=config.getInitParameter(pageSize);if(!(pazeS=null|.equals(pazeS)pageSize=Integer.parseInt(pazeS);public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 离婚财产公正协议书范本
- 注塑机设备租赁协议合同
- 永嘉专业会计代理协议书
- 汽车挂靠合同解除协议书
- 艺人签约合同之终止协议
- 电动摩托车租赁合同协议
- 混凝土浇灌施工合同范本
- 渡资产使用权合同或协议
- 腾讯产品包销合同协议书
- 汕尾打印机租赁协议合同
- 住建局停工通知回复函
- 2025年英语四级考试试卷及答案
- 造价咨询应急管理制度
- 在施工程以及近年已竣工工程合同履行情况
- 2025农商行借款合同模板
- 《PPP模式下的老旧小区博弈研究国内外文献综述5100字》
- 公转私提额合同范例
- 2025重庆市建筑安全员C证考试题库
- 二零二五年度特色美食街项目投资合作协议3篇
- 2024年安徽电气工程职业技术学院高职单招职业技能测验历年参考题库(频考版)含答案解析
- 人教版(2025新版)七年级下册数学第七章 相交线与平行线 单元测试卷(含答案)
评论
0/150
提交评论