如果想要完整的代码,请前往百度云网盘
链接: https://pan.baidu.com/s/1OTOO8GDFNR49eakZtR8Ikg 密码: 1au9
toolbean
DB.java
toolbean
DB.java
package com.zk.toolbean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.jms.TopicSession;
//import com.sun.java_cup.internal.internal_error;
//import com.sun.org.apache.commons.beanutils.converters.SqlDateConverter;
import com.zk.valuebean.ChoiceSingle;
import com.zk.valuebean.TempSingle;
import com.zk.valuebean.TopicSingle;
import com.zk.valuebean.VoteSingle;
public class DB {
private String className;
private String url;
private String username;
private String password;
private Connection con;
private Statement stm;
private ResultSet rs;
private PreparedStatement pstmt;
public DB() {
className = "com.mysql.jdbc.Driver";
url = "jdbc:mysql:///vote";
username = "root";
password = "1234";
}
/**
* @𶃋 º”‘ÿ ˝æ›ø‚«˝∂Ø≥ÖÚ
*/
public void loadDrive() {
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
System.out.println("º”‘ÿ ˝æ›ø‚«˝∂Ø≥Ã–Ú ß∞‹£°");
e.printStackTrace();
}
}
/**
* @𶃋 ªÒ»° ˝æ›ø‚¡¨Ω”
*/
public void getCon() {
loadDrive();
try {
con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println("¡¨Ω” ˝æ›ø‚ ß∞‹£°");
e.printStackTrace();
}
}
/**
* @𶃋 ªÒ»°Statement∂‘œÛ
*/
public void getStm() {
getCon();
try {
stm = con.createStatement();
} catch (Exception e) {
System.out.println("ªÒ»°Statement∂‘œÛ ß∞‹£°");
e.printStackTrace();
}
}
/**
* @𶃋 ≤È—Ø ˝æ›±Ì£¨ªÒ»°Ω·π˚ºØ
*/
public void getRs(String sql) {
getStm();
try {
rs = stm.executeQuery(sql);
} catch (Exception e) {
System.out.println("≤È—Ø ˝æ›ø‚ ß∞‹£°");
e.printStackTrace();
}
}
/*
* …æ≥˝ª∞°¢Ã̺”ª∞°¢—°œÓ
*/
public int deleteTopic(String sql){
getStm();
int rtn = 0;
try {
rtn = stm.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rtn;
}
public int deleteIP(String sql){
getStm();
int rtn = 0;
try {
rtn = stm.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rtn;
}
/**
* @𶃋 ≤È—Ø ˝æ›±Ì£¨ªÒ»°Õ∂∆±—°œÓ
*/
public List selectChoice(String sql){
List choiceList = null;
if (sql != null && !sql.equals("")) {
getRs(sql);
if (rs != null) {
choiceList = new ArrayList();
try {
while (rs.next()) {
ChoiceSingle choiceSingle = new ChoiceSingle();
choiceSingle.setChoice_id(rs.getInt(1));
choiceSingle.setChoice_num(rs.getInt(2));
choiceSingle.setChoice_conten(rs.getString(3));
choiceSingle.setTopic_id(rs.getInt(4));
choiceSingle.setChoice_vote(rs.getInt(5));
choiceList.add(choiceSingle);
}
} catch (Exception e) {
System.out.println("∑‚◊∞select_choice±Ì÷– ˝æ› ß∞‹£°");
e.printStackTrace();
} finally {
closed();
}
}
}
return choiceList;
}
/*
* ∑µªÿª∞Â√˚≥∆
*/
public String getTopicContent(String sql){
String name = "≤È’“≤ªµΩtopic√˚";
if(sql != null ){
getRs(sql);
if(rs != null){
try {
while(rs.next()){
name = rs.getString(2).toString();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return name;
}
/*
* ∑µªÿª∞Âid
*/
public int getTopicID(String sql){
int id = 0;
if(sql != null ){
getRs(sql);
if(rs != null){
try {
while(rs.next()){
id = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return id;
}
/**
* @𶃋 ≤È—Ø ˝æ›±Ì£¨ªÒ»°ª∞Â
*/
public List selectTopic(String sql){
List topicList = null;
if(sql != null && !sql.equals("")){
getRs(sql);
if(rs != null){
topicList = new ArrayList();
try {
while(rs.next()){
TopicSingle topicSingle = new TopicSingle();
topicSingle.setTopic_id(rs.getInt(1));
topicSingle.setTopic_name(rs.getString(2));
topicSingle.setTopic_voteTotal(rs.getInt(3));
topicList.add(topicSingle);
}
} catch (Exception e) {
System.out.println("∑‚◊∞topic ˝æ› ß∞‹£°");
e.printStackTrace();
}finally{
closed();
}
}
}
return topicList;
}
//≤È—Ø ˝æ›ø‚£¨ªÒ»°IP
public List selectIP(String sql){
List topicList = null;
if(sql != null && !sql.equals("")){
getRs(sql);
if(rs != null){
topicList = new ArrayList();
try {
while(rs.next()){
ChoiceSingle choiceSingle = new ChoiceSingle();
choiceSingle.setTopic_id(rs.getInt(1));
choiceSingle.setIps(rs.getString(2));
topicList.add(choiceSingle);
}
} catch (Exception e) {
System.out.println("∑‚◊∞IP ˝æ› ß∞‹£°");
e.printStackTrace();
}finally{
closed();
}
}
}
return topicList;
}
/**
* @𶃋 ≤È—Ø ˝æ›±Ì£¨ªÒ»°÷∏∂®IP◊Ó∫Û“ª¥ŒÕ∂∆±µƒº«¬º
*/
public TempSingle selectTemp(String sql) {
TempSingle tempSingle = null;
if (sql != null && !sql.equals("")) {
getRs(sql);
if (rs != null) {
try {
while (rs.next()) {
tempSingle = new TempSingle();
tempSingle.setId(MyTools.intToStr(rs.getInt(1)));
tempSingle.setVoteIp(rs.getString(2));
tempSingle.setVoteMSEL(rs.getLong(3));
tempSingle.setVoteTime(rs.getString(4));
}
} catch (Exception e) {
System.out.println("∑‚◊∞tb_temp±Ì÷– ˝æ› ß∞‹£°");
e.printStackTrace();
} finally {
closed();
}
}
}
return tempSingle;
}
/**
* @𶃋 ∏¸–¬ ˝æ›±Ì
*/
public int update(String sql) {
int i = -1;
if (sql != null && !sql.equals("")) {
getStm();
try {
i = stm.executeUpdate(sql);
} catch (Exception e) {
System.out.println("∏¸–¬ ˝æ›ø‚ ß∞‹£°");
e.printStackTrace();
} finally {
closed();
}
}
return i;
}
/**
* Ã̺”Õ∂∆±
*/
public boolean addVote(String topic_id,String choice_id){
int i=0;
int flag3 = 0;
String sql="update choice set choice_vote=choice_vote+1 where choice_id="+choice_id;
int flag1=update(sql);
// String sql2="select topic_voteTotal from topic where topic_voteTotal=NULL";
// String sql3="update topic set topic_voteTotal=0 where topic_id="+topic_id;
// List topic = this.selectTopic(sql2);
// if(!(topic.size()==0)){
// flag3=update(sql3);
// }
String sql1="update topic set topic_voteTotal=topic_voteTotal+1 where topic_id="+topic_id;
int flag2=update(sql1);
if(flag1>0&&flag2>0)return true;
else return false;
}
/**
* π‹¿Ì‘±µ«¬º≤È—Ø
*/
public String login(String sql){
String loginNot = "failed";
if (sql != null && !sql.equals("")) {
getStm();
try {
rs = stm.executeQuery(sql);
if(rs != null && rs.next()){
loginNot = "pass";
}
} catch (Exception e) {
System.out.println("≤È—Ø ˝æ›ø‚ ß∞‹£°");
loginNot = "failed";
e.printStackTrace();
}finally{
closed();
}
}
return loginNot;
}
/**
* @𶃋 πÿ±’ ˝æ›ø‚¡¨Ω”
*/
public void closed() {
try {
if (rs != null)
rs.close();
if (stm != null)
stm.close();
if (con != null)
con.close();
} catch (Exception e) {
System.out.println("πÿ±’ ˝æ›ø‚ ß∞‹£°");
e.printStackTrace();
}
}
}
Mytools.java
package com.zk.toolbean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyTools {
/**
* @功能 将int型数据转换为String型数据
* @参数 num为要转换的int型数据
* @返回值 String类型
*/
public static String intToStr(int num){
return String.valueOf(num);
}
/**
* @功能 比较时间。
* @参数 today当前时间,temp为上次投票时间。这两个参数都是以毫秒显示的时间
* @返回值 String类型
*/
public static String compareTime(long today,long temp){
int limitTime=60; //设置限制时间为60分钟
long count=today-temp; //计算当前时间与上次投票时间相差的毫秒数(该结果一定是大于等于0)
if(count<=limitTime*60*1000) //如果相差小于等于60分钟(1分=60秒,1秒=1000毫秒)
return "no";
else //如果相差大于60分钟
return "yes";
}
/**
* @功能 格式化时间为指定格式。首先通过Date类的构造方法根据给出的毫秒数获取一个时间,然后将该时间转换为指定格式,如"年-月-日 时:分:秒"
* @参数 ms为毫秒数
* @返回值 String类型
*/
public static String formatDate(long ms){
Date date=new Date(ms);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate=format.format(date);
return strDate;
}
}
PageTools.java
package com.zk.toolbean;
public class PageTools {
int totalPage=1;
int prePage=1;
int nextPage=1;
public int getPrePage(int currentPage ,int totalPage){
if(currentPage>1)
{
return currentPage - 1;
}
else
{
return 1;
}
}
public int getNextPage(int currentPage,int totalPage){
if(currentPage == totalPage)
{
return currentPage;
}
else {
return currentPage + 1;
}
}
public int getTotalPage(int amount,int totalTopic){
if(totalTopic%amount == 0)
{
return totalTopic/amount;
}
else {
return totalTopic/amount + 1;
}
}
}
valuebean
ChoiceSingle.java
package com.zk.valuebean;
public class ChoiceSingle {
int choice_id;
int choice_num;
String choice_conten;
int topic_id;
int choice_vote;
String ips;
/**
* @return the choice_vote
*/
public int getChoice_vote() {
return choice_vote;
}
/**
* @param choice_vote the choice_vote to set
*/
public void setChoice_vote(int choice_vote) {
this.choice_vote = choice_vote;
}
/**
* @return the choice_conten
*/
public String getChoice_conten() {
return choice_conten;
}
/**
* @param choice_conten the choice_conten to set
*/
public void setChoice_conten(String choice_conten) {
this.choice_conten = choice_conten;
}
/**
* @return the choice_id
*/
public int getChoice_id() {
return choice_id;
}
/**
* @param choice_id the choice_id to set
*/
public void setChoice_id(int choice_id) {
this.choice_id = choice_id;
}
/**
* @return the choice_num
*/
public int getChoice_num() {
return choice_num;
}
/**
* @param choice_num the choice_num to set
*/
public void setChoice_num(int choice_num) {
this.choice_num = choice_num;
}
/**
* @return the topic_id
*/
public int getTopic_id() {
return topic_id;
}
/**
* @param topic_id the topic_id to set
*/
public void setTopic_id(int topic_id) {
this.topic_id = topic_id;
}
public String getIps(){
return ips;
}
public void setIps(String ips){
this.ips=ips;
}
}
TempSingle.java
package com.zk.valuebean;
public class TempSingle {
private String id;
private String voteIp;
private long voteMSEL;
private String voteTime;
public long getVoteMSEL() {
return voteMSEL;
}
public void setVoteMSEL(long voteMSEL) {
this.voteMSEL = voteMSEL;
}
public String getVoteTime() {
return voteTime;
}
public void setVoteTime(String voteTime) {
this.voteTime = voteTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getVoteIp() {
return voteIp;
}
public void setVoteIp(String voteIp) {
this.voteIp = voteIp;
}
}
TopicSingle.java
package com.zk.valuebean;
public class TopicSingle {
int topic_id;
String topic_name;
int topic_voteTotal;
/**
* @return the topic_id
*/
public int getTopic_id() {
return topic_id;
}
/**
* @param topic_id the topic_id to set
*/
public void setTopic_id(int topic_id) {
this.topic_id = topic_id;
}
/**
* @return the topic_name
*/
public String getTopic_name() {
return topic_name;
}
/**
* @param topic_name the topic_name to set
*/
public void setTopic_name(String topic_name) {
this.topic_name = topic_name;
}
/**
* @return the topic_voteTotal
*/
public int getTopic_voteTotal() {
return topic_voteTotal;
}
/**
* @param topic_voteTotal the topic_voteTotal to set
*/
public void setTopic_voteTotal(int topic_voteTotal) {
this.topic_voteTotal = topic_voteTotal;
}
}
VoteSingle.java
package com.zk.valuebean;
public class VoteSingle {
private String id;
private String title;
private String num;
private String order;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
WebRoot
addTopic.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
if(session.getAttribute("adminName") == null){
session.setAttribute("mess","请先登录!");
response.sendRedirect("longin.jsp");
}
%>
<%
request.setCharacterEncoding("gb2312");
String topic = request.getParameter("topic");
String[] choice = request.getParameterValues("choice");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body><br>
<%
out.println(topic);
String sql = "insert into topic(topic_name) values ('"+topic+"')";
int rtn1 = myDb.deleteTopic(sql);
int count = myDb.getTopicID("select max(topic_id) from topic");
String sql3="insert into adress(topic_id) values ('"+count+"')";
int rtn2=myDb.deleteIP(sql3);
for(int i=0;i<choice.length;i++){
out.println(choice[i]);
if(choice[i] != null || choice[i] != ""){
String sql2 = "insert into choice(choice_num,choice_content,topic_id) values ("+i
+",'"+choice[i]+"',"+count+")";
myDb.deleteTopic(sql2);
}
}
session.setAttribute("mess","增加成功!");
response.sendRedirect("messages.jsp");
%>
</body>
</html>
admin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.util.List" %>
<%@ page import="com.zk.valuebean.TopicSingle" %>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<jsp:useBean id="pageTool" class="com.zk.toolbean.PageTools"/>
<%!
int totalPage=1;
int totalTopic=1;
int currentPage=1;
int prePage=1;
int nextPage=1;
int amount = 5;
%>
<%
if(session.getAttribute("adminName") == null){
session.setAttribute("mess","请先登录!");
response.sendRedirect("login.jsp");
}
%>
<html>
<head>
<script type="text/javascript">
var i=0;
var amount;
var tempRow=0;
var maxRows=0;
function doChange(){
if(document.form1.pageAmount.value != ""){
amount = document.form1.pageAmount.value;
window.location="admin.jsp?pN=1&a="+amount;
}
}
function addTable(){
var oTable2 = document.getElementById("table2");
i++;
var oTR = document.createElement("tr");
oTR.className = 'deletetr';
/* var oTD1 = document.createElement("td");
var oTD2 = document.createElement("td");
oTD1.innerHTML = '选项';
oTD2.innerHTML = '<input type="text" value="" size="22" />';
oTR.appendChild(oTD1);
oTR.appendChild(oTD2); */
oTR.innerHTML = '<td bgcolor="lightgrey" align="center">选项:</td><td width="650" align="center" ><input type="text" size="95" name="choice" id="choice"/></td>';
oTable2.appendChild(oTR);
}
function delTableRow(){
var row=document.getElementById("row");
var rownum = row.value;
if(rownum>1){
table2.deleteRow(rownum);
}
}
function add1(){
alert('我');
}
</script>
</head>
<body background="images/ba.jpg" width=100% height="100%">
<%
String toPage = request.getParameter("pN");
String toAmount = request.getParameter("a");
out.print(session.getAttribute("adminName")+",欢迎!");
if(toPage != null ){
currentPage = Integer.parseInt(toPage);
}
if(toAmount != null){
amount = Integer.parseInt(toAmount);
}
String sql = "select * from topic order by topic_id limit "+(currentPage-1)*amount+","+amount;
String sql2 = "select count(*) as COUNTS from topic";
//存储话题的集合
List topic = myDb.selectTopic(sql);
//话题数
totalTopic = myDb.getTopicID(sql2);
//页数
totalPage = pageTool.getTotalPage(amount,totalTopic);
%>
<a href="messages.jsp?quit=1">安全退出</a>
<form id="form1" name="form1">
<table border="1" align="center" width="800px">
<tr>
<th colspan="4" bgcolor="lightgrey">所有的话题:</th>
</tr>
<tr>
<td width="200px" bgcolor="lightgrey">编号</td>
<td width="440px" bgcolor="lightgrey">话题内容</td>
<td width="100px" bgcolor="lightgrey">已有票数</td>
<td width="60px" bgcolor="lightgrey">删除</td>
</tr>
<%if(topic.size() == 0 ){%>
<tr><th colspan="4">没有话题!</th></tr>
<%}else{
int i = 0;
while(i<topic.size()){
TopicSingle topicSingle = (TopicSingle)topic.get(i);
//上一页的页码
prePage = pageTool.getPrePage(currentPage,totalPage);
//下一页的页码
nextPage = pageTool.getNextPage(currentPage,totalPage);
%>
<tr>
<td><%=topicSingle.getTopic_id() %></td>
<td><%=topicSingle.getTopic_name() %></td>
<td><%=topicSingle.getTopic_voteTotal() %></td>
<td>
<a href="delete.jsp?id=<%=topicSingle.getTopic_id() %>">
删除
</a>
</td>
</tr>
<%
i++;
}
%>
<tr>
<td align="center" bgcolor="lightgrey">每页显示5项
</td>
<td colspan="5" align="center">
<a href="admin.jsp?pN=<%=prePage%>&a=<%=amount %>">上一页</a>
<%
for(int j=1;j<=totalPage;j++){
%>
<a href="admin.jsp?pN=<%=j%>&a=<%=amount %>"><%=j %></a>
<%
}
%>
<a href="admin.jsp?pN=<%=nextPage%>&a=<%=amount %>">下一页</a>
</tr>
<%
}
%>
</table>
</form>
<form name="form2" id="form2" method=post action="addTopic.jsp">
<table width="800px" id="table2" align="center" border="1">
<tr>
<td bgcolor="lightgrey" align="center" colspan="2">增加话题及相应选项
<input type="button" value="增加选项" onclick="addTable()"/>
<input type="button" value="减少选项" onclick="delTableRow()"/>
<input type=submit value="提交保存" />
<input type="text" value="" id="row"/>
</td>
</tr>
<tr>
<td bgcolor="lightgrey" align="center">话题:</td>
<td width="" align="center" ><input type="text" size="95" name="topic"/></td>
</tr>
<tr class="deletetr">
<td bgcolor="lightgrey" align="center">选项:</td>
<td width="650" align="center" ><input type="text" size="95" name="choice" id="choice"/></td>
</tr>
<tr class="deletetr">
<td bgcolor="lightgrey" align="center">选项:</td>
<td width="650" align="center" ><input type="text" size="95" name="choice" id="choice"/></td>
</tr>
<tr class="deletetr">
<td bgcolor="lightgrey" align="center">选项:</td>
<td width="650" align="center" ><input type="text" size="95" name="choice" id="choice"/></td>
</tr>
<tr class="deletetr">
<td bgcolor="lightgrey" align="center">选项:</td>
<td width="650" align="center" ><input type="text" size="95" name="choice" id="choice"/></td>
</tr>
</table>
</form>
</body>
</html>
delete.jsp
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<jsp:directive.page import="com.zk.toolbean.DB"/>
<%
if(session.getAttribute("adminName") == null){
session.setAttribute("mess","请先登录!");
response.sendRedirect("longin.jsp");
}
%>
<html>
<body>
<%
int topic_id = Integer.parseInt(request.getParameter("id"));
String sql = "delete from topic where topic_id="+topic_id;
String sql1 = "delete from choice where topic_id="+topic_id;
int rtn1 = myDb.deleteTopic(sql);
int rtn2 = myDb.deleteTopic(sql1);
System.out.println("rtn1="+rtn1+",rtn2="+rtn2);
session.setAttribute("mess","已经成功删除!");
response.sendRedirect("messages.jsp");
%>
</body>
</html>
doVote.jsp
<%@page import="com.zk.valuebean.ChoiceSingle"%>
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ page import="com.zk.valuebean.TempSingle" %>
<%@ page import="com.zk.toolbean.MyTools" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<jsp:directive.page import="com.zk.toolbean.DB"/>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<%
String selectId=request.getParameter("ilike");
String topic_id = request.getParameter("tc_id");
String mess="";
String clintIP=request.getRemoteAddr();
System.out.println(clintIP);
String sql="select topic_id,ips from adress";
List ipss= myDb.selectIP(sql);
int i=0;
int flag=0;
int topicId=Integer.parseInt(topic_id);
System.out.println(ipss.size());
while(i<ipss.size()){
ChoiceSingle ip1=(ChoiceSingle)ipss.get(i);
if(ip1.getTopic_id()==topicId){
System.out.println(ip1.getIps());
if(ip1.getIps().equals(clintIP)){
flag=0;
}else{
myDb.addVote(topic_id, selectId);
String sql2="update adress set ips="+"'"+clintIP+"'"+"where topic_id="+topicId;
myDb.update(sql2);
flag=1;
break;
}
}
i++;
}
if(flag==1){
mess="投票成功";
session.setAttribute("mess", mess);
response.sendRedirect("messages.jsp?tp_id="+topic_id);
}else{
mess="投票失败,您已经投过票了";
session.setAttribute("mess", mess);
response.sendRedirect("messages.jsp?tp_id="+topic_id);
}
%>
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>在线投票</title>
<link style="text/css" rel="stylesheet" href="css/style.css">
</head>
<body bgcolor="#F0F0F0" background="images/ba.jpg" width=100% height="100%">
<center>
<table border="0" width="760" height="620" cellspacing="0" cellpadding="0" >
<tr>
<td style="padding-top:152;padding-left:120">
<a href="showTopic.jsp"><img src="images/show.png" style="border:0; height: 207px; width: 229px"></a>
<a href="login.jsp"><img src="images/admina.png" style="border:0; height: 207px; width: 229px"></a>
</td>
</tr>
</table>
</center>
</body>
</html>
login.jsp
a<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>在线投票系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="#F0F0F0" background="images/ba.jpg" width=100% height="100%">
<form action="login.jsp" method="post">
<center>
<table>
<tr>
<td>UserName</td> <td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>PassWord</td> <td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="login"/></td>
<td><input type="reset" value="Reset"/>
</tr>
</table>
</center>
</form>
<%
request.setCharacterEncoding("gb2312");
if(request.getParameter("submit") != null){
String name = request.getParameter("name");
String pswd = request.getParameter("password");
String sql = "select * from admin where name='"+name+"' and password='"+pswd+"'";
String logRs = myDb.login(sql);
if(logRs.equals("pass")){
session.setAttribute("adminName",name);
response.sendRedirect("admin.jsp");
}
else{
out.print("<p align=center><font color=red>��¼ʧ��</font></p>");
}
}
%>
</body>
</html>
showTopic.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.util.List" %>
<%@ page import="com.zk.valuebean.TopicSingle" %>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<jsp:useBean id="pageTool" class="com.zk.toolbean.PageTools"/>
<%!
int totalPage=1;
int totalTopic=1;
int currentPage=1;
int prePage=1;
int nextPage=1;
int amount = 5;
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body background="images/ba.jpg" width=100% height="100%">
<%
String toPage = request.getParameter("pN");
String toAmount = request.getParameter("a");
if(toPage != null ){
currentPage = Integer.parseInt(toPage);
}
if(toAmount != null){
amount = Integer.parseInt(toAmount);
}
String sql = "select * from topic order by topic_id limit "+(currentPage-1)*amount+","+amount;
String sql2 = "select count(*) as COUNTS from topic";
List topic = myDb.selectTopic(sql);
totalTopic = myDb.getTopicID(sql2);
totalPage = pageTool.getTotalPage(amount,totalTopic);
%>
<form id="form1" name="form1">
<table border="1" align="center" width="800px">
<tr>
<th colspan="5" bgcolor="lightgrey">现有话题:</th>
</tr>
<tr>
<td bgcolor="lightgrey">编号</td>
<td bgcolor="lightgrey">话题内容</td>
<td bgcolor="lightgrey">已有票数</td>
<td bgcolor="lightgrey">详情</td>
<td bgcolor="lightgrey">投票</td>
</tr>
<%if(topic.size() == 0 ){%>
<tr><th colspan="4">没有话题!</th></tr>
<%}else{
int i = 0;
while(i<topic.size()){
TopicSingle topicSingle = (TopicSingle)topic.get(i);
prePage = pageTool.getPrePage(currentPage,totalPage);
nextPage = pageTool.getNextPage(currentPage,totalPage);
%>
<tr>
<td><%=topicSingle.getTopic_id() %></td>
<td><%=topicSingle.getTopic_name() %></td>
<td><%=topicSingle.getTopic_voteTotal() %></td>
<td>
<a href="showVote.jsp?id=<%=topicSingle.getTopic_id() %>">
查看
</a>
</td>
<td>
<a href="vote.jsp?id=<%=topicSingle.getTopic_id() %>">投票</a>
</td>
</tr>
<%
i++;
}
%>
<tr>
<td align="center" bgcolor="lightgrey">每页显示5项
</td>
<td colspan="5" align="center">
<a href="showTopic.jsp?pN=<%=prePage%>&a=<%=amount %>">上一页</a>
<%
for(int j=1;j<=totalPage;j++){
%>
<a href="showTopic.jsp?pN=<%=j%>&a=<%=amount %>"><%=j %></a>
<%
}
%>
<a href="showTopic.jsp?pN=<%=nextPage%>&a=<%=amount %>">下一页</a>
</tr>
<%
}
%>
</table>
<center>
<br>
<br>
<a href="index.jsp"><img src="images/indexB.jpg" ></a>
</center>>
</form>
<script type="text/javascript">
var amount;
function doChange(){
if(document.form1.pageAmount.value != ""){
amount = document.form1.pageAmount.value;
window.location="showTopic.jsp?pN=1&a="+amount;
}
}
</script>
</body>
</html>
showVote.jsp
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ page import="java.util.List" %>
<%@ page import="com.zk.valuebean.ChoiceSingle" %>
<%@ page import="java.awt.Font" %>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<%
float numAll=0;
int topic_id = Integer.parseInt(request.getParameter("id"));
System.out.println("话题编号为:"+topic_id);
String sql1 = "select * from topic where topic_id="+topic_id;
String topic_content = myDb.getTopicContent(sql1);
String sql="select * from choice where topic_id="+topic_id+" order by choice_id";
List showlist=myDb.selectChoice(sql);
%>
<html>
<head>
<title>在线投票</title>
<link style="text/css" rel="stylesheet" href="css/style.css">
</head>
<body background="images/ba.jpg" width=100% height="100%">
<center>
<table border="0" cellspacing="0" cellpadding="0" width="760" height="620" >
<tr height="20">
<td valign="top" width="40%">
<table border="0" width="75%" cellspacing="0" cellpadding="0" style="margin-top:310;margin-left:45">
<% if(showlist==null||showlist.size()==0){ %>
<tr height="200"><td align="center" colspan="2">没有选项可显示!</td></tr>
<%
}
else{
int i=0;
while(i<showlist.size()){
ChoiceSingle single=(ChoiceSingle)showlist.get(i);
%>
<tr height="25">
<td><%=single.getChoice_conten() %></td>
<td width="25%" align="right"><%=single.getChoice_vote() %> 票 </td>
</tr>
<%
i++;
}
}
%>
<tr height="200">
<br>
<td colspan="2">
<center>
<a href="index.jsp"><img src="images/indexB.jpg" ></a>
</center>
</td>
</tr>
</table>
</td>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
vote.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="com.zk.valuebean.ChoiceSingle" %>
<jsp:useBean id="myDb" class="com.zk.toolbean.DB"/>
<%
response.addHeader("Pragma","No-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expires",1L);
int topic_id =Integer.parseInt(request.getParameter("id"));
String sql="select * from choice where topic_id="+topic_id;
String sql1 = "select * from topic where topic_id="+topic_id;
List votelist=myDb.selectChoice(sql);
String topicName = myDb.getTopicContent(sql1);
%>
<html>
<head>
<title>在线投票</title>
<link style="text/css" rel="stylesheet" href="css/style.css">
</head>
<body bgcolor="#F0F0F0" background="images/ba.jpg" width=100% height="100%">
<center>
<form action="doVote.jsp?tc_id=<%=topic_id %>" method="post">
<table border="0" width="760" height="620">
<tr height="20">
<!-- 显示投票选项 -->
<td valign="top" width="420">
<table border="0" cellspacing="0" cellpadding="0" width="100%" style="margin-top:240;margin-left:51" >
<tr height="75">
<td align="left" colspan="2" valign="bottom">
<font size="5"><%out.println(topicName); %></font>
</td>
</tr>
<!-- 如果集合为空 -->
<% if(votelist==null||votelist.size()==0){ %>
<tr height="200"><td align="center" colspan="2">没有选项可显示!</td></tr>
<!-- 如果集合不为空 -->
<%
}
else{
%>
<tr>
<td align="center" width="60%">
<table border="0" width="100%">
<%
int i=0;
while(i<votelist.size()){
ChoiceSingle single=(ChoiceSingle)votelist.get(i);
%>
<tr height="27">
<td style="text-indent:7"><%=single.getChoice_conten() %></td>
<td width="30%" align="center"><input type="radio" name="ilike" value="<%=single.getChoice_id() %>"></td>
</tr>
<%
i++;
}
%>
</table>
</td>
<td valign="top">
<b><font color="white">注意事项:</font></b>
<p><font color="#FDE401"><li>1小时只能投同一话题一次票!</li></font>
</td>
</tr>
<%
}
%>
<!-- 显示操作按钮 -->
<tr height="97">
<td align="center" valign="top" colspan="2">
<input type="submit" value="" style="background-image:url(images/submitB.jpg);width:68;border:0;height:26 ">
<input type="reset" value="" style="background-image:url(images/resetB.jpg);width:68;border:0;height:26">
<br>
<a href="showVote.jsp?id=<%=topic_id %>"><img src="images/showB.jpg" ></a>
<a href="index.jsp"><img src="images/indexB.jpg" ></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
好了就这些,如果觉得好,点个赞。