您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > jsp简单编写投票系统
无聊之余练练jsp简单实现一个投票系统jsp+mssql+javabean首先规划下系统整体:Conn.java(数据库连接的java类文件)index.jsp(首页)vote.jsp(投票提交,sql执行对投票数进行累加)s.jsp(投票成功后提示并转到结果显示页)result.jsp(显示投票结果)add.jsp(可以增加项,也就是要投票的项,这里是大学的名字)addvote.jsp(sql执行)del.jsp(删除投票项)delcl.jsp(sql执行删除操作)style.css(css控制)实现过程:首先创建一个数据库edu.表education字段为:name(学校的名字),num(票数)createdatabaseedu(创建库)useeducreatetableeducation(idintidentity(1,1)primarykey,namechar(20)notnull,numintnotnull)这里用到sql的驱动为:jtds-0.8.1.jar(1)创建Conn.java类文件packagecom;importjava.sql.*;publicclassConn{Stringurl,sql;Connectioncon=null;Statementstmt=null;ResultSetrs=null;publicConn(){try{Class.forName(net.sourceforge.jtds.jdbc.Driver);url=jdbc:jtds:sqlserver://localhost:1433/edu;con=DriverManager.getConnection(url,sa,);stmt=con.createStatement();}catch(Exceptione){e.printStackTrace();}}publicResultSetexecuteQuery(Stringsql){try{rs=stmt.executeQuery(sql);}catch(SQLExceptione){System.err.println(e.getMessage());}returnrs;}publicintexecuteUpdate(Stringsql){intresult=0;try{result=stmt.executeUpdate(sql);}catch(SQLExceptione){e.getMessage();}returnresult;}}(2)创建index.jsp%@pagecontentType=text/html;charset=gb2312language=javaimport=java.sql.*errorPage=%jsp:useBeanid=voteBeanscope=pageclass=com.Conn/htmlheadtitle/titlemetahttp-equiv=text/html;charset=gb2312linkrel=stylesheettype=text/csshref=style.css/headbodydivalign=center请选择你喜欢的学校投票/div%request.setCharacterEncoding(gb2312);%%!Stringsql;ResultSetrs;%%sql=select*fromeducation;rs=voteBean.executeQuery(sql);%centertableformaction=vote.jspmethod=post%while(rs.next()){%trtdinputtype=radioname=namevalue=%=rs.getString(id)%%=rs.getString(name)%/td/tr%}rs.close();%trtdinputtype=submitvalue=投票/td/tr/formtrtdahref=result.jsp查看结果/a/td/tr/table/center/body/html(3)创建Vote.jsp%@pagecontentType=text/html;charset=gb2312%%@pageimport=java.sql.*language=java%jsp:useBeanid=voteBeanscope=pageclass=com.Conn/%Stringname=request.getParameter(name);if(name!=null&&name!=){Stringsql=updateeducationsetnum=num+1whereid=+name;voteBean.executeUpdate(sql);}%jsp:forwardpage=s.jsp/(4)s.jsp(投票后提示信息并转向结果显示)%@pagecontentType=text/html;charset=gb2312%htmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=gb2312title投票/title/headscriptlanguage=javascriptalert(投票成功,请查看结果!!);window.location.href='result.jsp';/scriptbody/body/html(5)result.jsp(结果显示页)%@pagecontentType=text/html;charset=gb2312language=javaimport=java.sql.*errorPage=%jsp:useBeanid=voteBeanscope=pageclass=com.Conn/htmlheadtitle投票结果/titlemetahttp-equiv=Content-Typecontent=text/html;charset=gb2312linkrel=stylesheettype=text/csshref=style.css/headbody%!inttotalSum=0;ResultSetrs=null;Stringsql;%centertable%rs=voteBean.executeQuery(select*fromeducation);inti=1;while(rs.next()){%trtd选项%=i%:%=rs.getString(name)%/td/tr%totalSum=rs.getInt(num);%trtd得:[%=totalSum%票]/td/tr%i++;}%/table/center%rs.close();%divalign=rightahref=index.jsp返回投票/a/div/body/html(6)add.jsp(添加页面)%@pagelanguage=javacontentType=text/html;charset=gb2312%htmlheadtitle添加学校/title/headbodycentertabletrtdcolspan=3bgcolor=#99CCFF增加/td/trtrtdcolspan=3formaction=addvote.jspmethod=postp内容:inputtype=textname=name1/pinputtype=submitname=submitvalue=提交inputtype=resetname=resetvalue=重置/form/td/trtrtdahref=index.jsp投票中心/a/td/tr/table/center/body/html(7)addvote.jsp(添加执行操作)%@pagecontentType=text/html;charset=gb2312%%@pagelanguage=javaimport=java.sql.*%jsp:useBeanid=voteBeanscope=pageclass=com.Conn/htmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=gb2312/headbody%!Stringname;ResultSetrs;Stringsql;%%request.setCharacterEncoding(gb2312);name=request.getParameter(name1);if(name!=null&&name!=){sql=insertintoeducation(name,num)values('+name+',0);voteBean.executeUpdate(sql);}%/body/htmljsp:forwardpage=del.jsp/(8)del.jsp(删除页面)%@pagelanguage=javacontentType=text/html;charset=gb2312%%@pageimport=java.sql.*%jsp:useBeanid=voteBeanscope=pageclass=com.Conn/htmlheadtitle修改,删除操作/titlemetahttp-equiv=Content-Typecontent=text/html;charset=gb2312linkrel=stylesheettype=text/csshref=style.css/head%!ResultSetrs;Stringsql;%bodydivalign=center%request.setCharacterEncoding(gb2312);sql=selectid,namefromeducationorderbyid;rs=voteBean.executeQuery(sql);%tablewidth=60%border=0cellspacing=1cellpadding=2trtdwidth=10%bgcolor=#99CCFFdivalign=center序号/div/tdtdwidth=30%bgcolor=#99CCFFdivalign=center学校/div/tdtdwidth=10%bgcolor=#99CCFFdivalign=center删除操作/div/td/tr%inti=1;while(rs.next()){%trtddivalign=center%=i%/div/tdtddivalign=center%=rs.getString(name)%/div/tdtddivalign=centerahref=delcl.jsp?id=%=rs.getInt(id)%删除/a/div/td/tr%i=i+1;}%/table/div/body/html(9)delcl.jsp(执行删除操作)%@pagelanguage=javaimport=java.sql.*contentType=text/html;chars
本文标题:jsp简单编写投票系统
链接地址:https://www.777doc.com/doc-5482781 .html