您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > 学生信息管理系统JSP版(Eclipse)
学生信息管理系统JSP版(Eclipse)1、新建new-DynamicWebProject-名字为StudentJSP。2、准备工作1)编写JSP测试文件,名称为hello.jsp修改hello.jsp的charset为UTF-8加入代码:bodyHello!/body代码:%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbodyHello!/body/html附:修改默认字符集步骤:Windows-Preference-Web-JSPfiles-将Encoding改为UTF-82)右击项目-Properties-Resurce-修改字符集为UTF-8.3)右击项目-Properties-JavaBuildPath改为如图(需先在WEB-INF下新建classes)4)将MySQL中一个jar包复制到/WEB-INF/lib下,如图:3、在/WebContent下new一个JSPfile,名字为display0.jsp代码如下:%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%@pageimport=java.sql.*%//(注意要引入sql的包)!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbody%StringconnStr=jdbc:mysql://localhost:3306/yStudent?characterEncoding=utf8;Connectionconn=null;Statementstmt=null;ResultSetrs;try{Class.forName(com.mysql.jdbc.Driver);conn=DriverManager.getConnection(connStr,root,123456);stmt=conn.createStatement();}catch(Exceptionex){ex.printStackTrace();System.out.println(exceptioningetConnection);}Stringsql=select*fromstudentinfo;//表明为studentinfotry{rs=stmt.executeQuery(sql);%tablealign=centerwidth=600border=1trth编号/thth姓名/thth年龄/th/tr%while(rs.next()){%trtd%=rs.getString(id)%/tdtd%=rs.getString(name)%/tdtd%=rs.getInt(age)%/td/tr%}%/table%stmt.close();conn.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}%%/body/html4、在项目下新建一个文件夹DataBase,将用到的学生数据库脚本文件复制到此目录下5、启动mysql,Tomcat],运行测试display0.6、改进版本display11)将display0.jsp复制粘贴到本项目的/WebCont下并修改命名为display1.jsp2)在/src下new一个新包,包名为com.ls.student.bean并在此包下新建两个新类来处理SQL的Helper。SQLHelper.java代码如下:packagecom.ls.student.bean;importjava.sql.*;//引入sql相关包publicclassSQLHelper{privateStringdriverStr=com.mysql.jdbc.Driver;privateStringconnStr=jdbc:mysql://localhost:3306/yStudent?characterEncoding=utf8;privateStringdbusername=root;privateStringdbpassword=123456;privateConnectionconn=null;privateStatementstmt=null;publicSQLHelper(){try{Class.forName(driverStr);conn=DriverManager.getConnection(connStr,dbusername,dbpassword);stmt=conn.createStatement();}catch(Exceptionex){System.out.println(exceptioninSQLHelper);}}publicintexecuteUpdate(Stringsql){intresult=0;try{result=stmt.executeUpdate(sql);}catch(Exceptionex){System.out.println(ExceptioninexecuteUpdate);}returnresult;}publicResultSetexecuteQuery(Stringsql){ResultSetrs=null;try{rs=stmt.executeQuery(sql);if(!rs.next())returnnull;rs.previous();}catch(Exceptionex){System.out.println(ExceptioninexecuteQuery);}returnrs;}publicvoidclose(){try{stmt.close();conn.close();}catch(Exceptione){}}}在此包下新建student类,代码如下packagecom.ls.student.bean;publicclassstudent{privateStringid,name;privateintage;publicstudent(){super();}publicstudent(Stringid,Stringname,intage){super();this.id=id;this.name=name;this.age=age;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){returnStudent[id=+id+,name=+name+,age=+age+];}}3)display1.jsp代码修改如下:注意先引入java.sql.*包和com.ls.student.bean包主要代码修改为body/body内的修改,代码如下:%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%@pageimport=java.sql.*;importcom.ls.student.bean.*%//引入包!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbody%SQLHelperdb=newSQLHelper();Stringsql=select*fromstudentinfo;ResultSetrs=db.executeQuery(sql);%tablealign=centerwidth=600border=1trth编号/thth姓名/thth年龄/thth操作/th/tr%while(rs.next()){%trtd%=rs.getString(id)%/tdtd%=rs.getString(name)%/tdtd%=rs.getInt(age)%/tdtddivalign=centerahref=update.jsp?id=%=rs.getString(id)%修改/a ahref=delete.jsp?id=%=rs.getString(id)%删除/a/div/td/tr%}%/table%db.close();%/body/html7、实现增加功能新建insert.jsp文件%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%@pageimport=java.sql.*%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInsertstudent/title/headbody%@includefile=head.jsp%%request.setCharacterEncoding(UTF-8);Stringid=request.getParameter(id);Stringname=request.getParameter(name);intage=Integer.parseInt(request.getParameter(age));Stringsql=insertintostudentinfovalues('+id+','+name+',+age+);Class.forName(com.mysql.jdbc.Driv
本文标题:学生信息管理系统JSP版(Eclipse)
链接地址:https://www.777doc.com/doc-6911590 .html