只要一个界面 ,但是建议写两个,一个用于显示投票界面 ,一个用于接受用户的投票,将所对应的老师得票加1 ,
工作完了再回到前一个界面 。
各个页面与作用:
votedao.java 连接数据库,查询老师的编号 ,姓名 ,得票,修改老师的得票数,将编号对应的得票 数加 1
vote.java 封装老师的编号 ,姓名 ,得票 数
display.jsp 调用votedao查询老师编号 ,姓名 ,得票数,并显示
vote.jsp 获得request 传送 过来的老师编号数组 ,调用 votedao 将每个编号对应的得票数加1 ,回到display.jsp
二。数据库初始化
属性:teacherno teachername votenumber
在mysql中写成脚本运行,用source 命令。
use test;create table t_vote( teacherno varchar(20), teachername varchar(20), vote int );insert into t_vote values('1', 'lishengjing',1);insert into t_vote values('2', 'huangwenjun',1);insert into t_vote values('3', 'zhaosy',2);insert into t_vote values('4', 'chaoshehn',3);
三。java 代码
要将mysql的驱动复制到 web-inf/lib中,将bar.jpg 复制到webroot下的img目录。导入包时最好用 import java.sql*;
public class votedao { private connection conn=null; public void initconnection() throws exception{ class.forname(com.mysql.jdbc.driver); string url =jdbc:mysql://localhost:3306/test; conn = drivermanager.getconnection(url , root,root); } public arraylist getallvotes()throws exception{ arraylist all = new arraylist (); initconnection(); string sql = select * from t_vote; statement stat= conn.createstatement(); resultset rs = stat.executequery(sql); while (rs.next()){ vote vote = new vote(); vote.setteacherno(rs.getstring(teacherno)); vote.setteachername(rs.getstring(teachername)); vote.setvotenumber(rs.getint(vote)); all.add(vote); } clossconnection(); return all; } public void updatevotes(string [] teacherno )throws exception{ initconnection(); string sql = update t_vote set vote=vote+1 where teacherno=?; preparedstatement ps = conn.preparestatement(sql); for (int i =0;i
display.jsp
欢迎投票 编号 姓名 得票数
vote,jsp