选课管理系统代码Java 选课管理系统代码

java学生选课系统学生选课系统的设计与实现摘要本文以一个具体的应用系统—学生选课信息系统的设计与实现来说明如何利用UML和EJB组件来设计和构建分布式软件系统平台 。UML和组件技术结合使用能提高开发效率,增加系统的易维护性 。关键词UML;EJB;实例1引言现在信息管理系统软件的开发,采用组件技术可以提高效率,信息管理系统的分析设计也采用UML来进行 。两者的结合可以极大的提高开发效率,保证系统开发的易维护性 。本文用UML这种设计方法和EJB这种组件技术来设计和实现一个系统 。2系统分析本系统设计为学生通过网页登陆学校网站,进行选课 。下面用用例图来说明该系统要实现的功能 。2.1用例图2.2系统总体结构图本系统采用三层体系结构,分为表示层,事务处理层,数据存储层 。三层结构层次清晰,易维护 。图3类图学生选课系统涉及到三个实体类学生,课程,以及学生和课程之间的一个一对多关系类 。对每一个类,映射到一张表 。学生类和课程类用Container-ManagedEntityBean实现,学生和课程间的一对多关系类,用Bean-ManagedPersistence的EntityBean实现 。再设计一个SessionBean对学生选课过程进行控制 。页面显示部分用JSP实现 。3数据库设计学生表对应学生实体,详细内容如下表1学生表关联表对应学生和课程间的一对多关系,详细内容如下研究开发4实现4.1SessionBean的设计4.1.1定义HomeInterface4.1.2定义RemoteInterfacepublicinterfaceEnrollSessionextendsEJBObject{//-----//这是一个基于SessionBean的Remote接口,这个SessionBean是基于//Stateful的SessionBean,用来对特定学生选课的登记过程进行操作//-----------------------------publicStringgetStudentName()throwsRemoteException;publicvoidenroll(ArrayListcourseItems)throwsRemoteExcep-tion;publicvoidunenroll()throwsRemoteException;publicvoiddeleteStudent()throwsFinderException,RemoteEx-ception;publicvoiddeleteCourse(Stringcourse_id)throwsRemoteExcep-tion;}4.1.3Client获取HomeInterface和RemoteInterface的参考方式,选课管理系统代码Java我们使用JNDI机制来获取Home接口和Remote接口的对象参考 。4.1.4定义回调方法4.1.5实现远程数据库的连接使用JNDI机制,通过数据库的JNDI名称javacomp/env/jdbc/StudentCourseDB来连接后台数据库 。4.1.6SessionBean方法实现//定义变量publicStudentHomesHome;publicEnrollHomeeHome;publicStringstudent_id;publicStringname;//回调方法实现publicvoidejbCreate(Stringstudent_id)throwsCreateException{try{Studentstudent=sHome.findByPrimaryKey(student_id);name=student.getName();}catch(ObjectNotFoundExceptione){thrownewCreateException(Student+student_id+notfoundinStudentTBL!);}catch(Exceptione){thrownewEJBException(e.getMessage());}this.student_id=student_id;}//商业方法实现publicvoidenroll(ArrayListcourseItems){Enrollenroll=null;try{enroll=eHome.findByPrimaryKey(student_id);}catch(Exceptione){}try{if(enroll!=null){enroll.replaceCourseItems(courseItems);}else{eHome.create(student_id,courseItems);}}catch(Exceptione){thrownewEJBException(e.getMessage());}}publicvoidunenroll(){try{Enrollenroll=eHome.findByPrimaryKey(student_id);enroll.remove();}catch(Exceptione){thrownewEJBException(e.getMessage());}}//涉及到对两张表的删除 。publicvoiddeleteStudent()throwsFinderException{try{Enrollenroll=eHome.findByPrimaryKey(student_id);Studentstudent=sHome.findByPrimaryKey(student_id);enroll.remove();student.remove();}catch(Exceptione){thrownewEJBException(e.getMessage());}}publicvoiddeleteCourse(Stringcourse_id){PreparedStatementps=null;try{getConnection();StringdeleteStatement=deletefromEnrollTBL+wherestudent_id=?andcourse_id=?;ps=con.prepareStatement(deleteStatement);ps.setString(1,student_id);ps.setString(2,course_id);ps.executeUpdate();}catch(Exceptione){thrownewEJBException(e.getMessage());}finally{try{ps.close();con.close();}catch(Exceptione){thrownewEJBException(e.getMessage());}}}4.2EntityBean的设计我们以关联表(EnrollTBL)对应的实体Bean为例进行说明,它涉及到两个表的一对多关系 。4.2.1定义Home接口4.2.2定义RemoteInterfacepublicinterfaceEnrollextendsEJBObject{//---------------//这是一个基于EntityBean的Remote接口,这个EntityBean是基于//Bean-ManagedPersistence的EntityBean,用来对EnrollTBL表进行操作//--------------------------publicArrayListgetCourseItems()throwsRemoteException;publicStringgetStudent_id()throwsRemoteException;publicvoidreplaceCourseItems(ArrayListcourseItems)throwsRemoteException;}4.2.3变量定义publicStringstudent_id;publicArrayListcourseItems;4.2.4增加数据记录实现publicStringejbCreate(Stringstudent_id,ArrayListcourseItems)throwsCreateException{if(courseItems==null||courseItems.size()==0){thrownewCreateException(ejbCreateexception!);}this.student_id=student_id;try{enroll(courseItems);}catch(Exceptione){thrownewEJBException(ejbCreateexception+e.getMessage());}this.courseItems=courseItems;returnstudent_id;}//根据学生ID,插入课程项 。privatevoidenroll(ArrayListcourseItems)throwsSQLException{StringinsertStatement=insertintoEnrollTBLvalues(?,?);PreparedStatementps=con.prepareStatement(insertStatement);try{//------------//依次将所有的课程项目插入EnrollTBL表//------------------ps.setString(1,this.student_id);for(inti=0;icourseItems.size();i++){Stringcourse_id=(String)courseItems.get(i);ps.setString(2,course_id);ps.executeUpdate();}}finally{ps.close();}}//根据学生ID,删除课程项privatevoidunenroll()throwsSQLException{StringdeleteStatement=deletefromEnrollTBL+wherestudent_id=?;PreparedStatementps=con.prepareStatement(deleteStatement);try{ps.setString(1,student_id);ps.executeUpdate();}finally{ps.close();}}5部署和运行5.1部署程序5.1.1部署StudentEntityBean,设置事务属性,生成部署文件 。5.1.2同样,再部署其他三个EJB组件EnrollSessionBean,CourseEntityBean,EnrollEntityBean 。5.1.3部署web组件5.1.4部署整个应用程序5.2运行启动J2EE服务器,启动数据库服务器,打开浏览器 。学生登录,即可选课 。6结论组件技术使得复杂的多层结构应用系统开发变得容易 。采用组件技术能提高开发人员的效率,降低软件的开发和维护成本,提高软件的质量,控制所构建系统的复杂性 。UML设计方法的使用能提高软件设计的效率和保证设计的规范性 。参考文献[1]CraigLarman.UML和模式应用-面向对象分析与设计导论[M].北京机械工业出版社,2003.10-100.[2]刘特.J2EEEJB应用编程实例[M].北京清华大学出版社,2003.90-150.[3]GeriSchneider.用例分析技术[M].北京机械工业出版社,2003.1-98.
-----------------------------------------------

推荐阅读