简单的投票系统,但是显示的时候出现问题,希望高手解答。

10-10-23 tiansong163
vote.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<html>

<head>

<title>投票选择</title>

</head>

<body>

<%

Connection con = null;

Statement sql = null;

ResultSet rs = null;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

out.println("加载不成功...");

}

try {

con = DriverManager.getConnection("jdbc:odbc:vote", "", "");

sql = con.createStatement();

rs = sql.executeQuery("select *from people");

out.print("<form action=startvote.jsp method=post >");

out.print("<table border>");

out.print("<tr>");

out.print("<td>");

out.print("姓名");

out.print("</td>");

out.print("<td>");

out.print("投票选择");

out.print("</td>");

out.print("</tr>");

while (rs.next()) {

String name = rs.getString(1);

out.print("<tr>");

out.print("<td>");

out.print(name);

out.print("</td>");

out.print("<td>");

out.print("<input type=radio name=name >");

out.print("</td>");

out.print("</tr>");

}

out.print("</table>");

out.print("<input type=submit value=提交>");

out.print("</form>");

rs.close();

sql.close();

con.close();

} catch (Exception e) {

out.println("连接不成功...");

}

%>

</body>

</html>

startvote.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<html>

<head>

<title>投票选择</title>

</head>

<body>

<%

Connection con = null;

Statement sql = null;

ResultSet rs = null;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

out.println("加载不成功...");

}

try {

con = DriverManager.getConnection("jdbc:odbc:vote", "", "");

sql = con.createStatement();

rs = sql.executeQuery("select *from people");

out.print("<form action=startvote.jsp method=post >");

out.print("<table border>");

out.print("<tr>");

out.print("<td>");

out.print("姓名");

out.print("</td>");

out.print("<td>");

out.print("投票选择");

out.print("</td>");

out.print("</tr>");

while (rs.next()) {

String name = rs.getString(1);

out.print("<tr>");

out.print("<td>");

out.print(name);

out.print("</td>");

out.print("<td>");

out.print("<input type=radio name=name >");

out.print("</td>");

out.print("</tr>");

}

out.print("</table>");

out.print("<input type=submit value=提交>");

out.print("</form>");

rs.close();

sql.close();

con.close();

} catch (Exception e) {

out.println("连接不成功...");

}

%>

</body>

</html>


1
SpeedVan
2010-10-28 20:51
vote.jsp和startvote.jsp代码一样的?

tiansong163
2010-10-28 22:54
<%@ page contentType="text/html;charset=GB2312"%>

<%@ page import="java.sql.*"%>

<%@ page import="java.io.*"%>

<html>

<body>

<%!//记录总票数的变量:

int total = 0;

// 操作总票数的同步方法:

synchronized void countTotal() {

total++;

}%>

<%

boolean vote = true;//决定用户是否有权投票的变量。

// 得到被选择的候选人名字:

String name = "";

name = request.getParameter("name");

if (name == null) {

name = "?";

}

byte a[] = name.getBytes("ISO-8859-1");

name = new String(a);

// 得到投票人的IP 地址:

String IP = (String) request.getRemoteAddr();

// 加载桥接器:

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException e) {

}

Connection con = null;

Statement sql = null;

ResultSet rs = null;

// 首先查询IP 表,判断该用户的IP 地址是否已经投过票:

try {

con = DriverManager.getConnection("jdbc:odbc:vote", " ", " ");

sql = con.createStatement();

rs = sql.executeQuery("SELECT * FROM IP WHERE IP = " + "'" + IP

+ "'");

int row = 0;

while (rs.next()) {

row++;

}

if (row >= 1) {

vote = false; // 不允许投票。

}

} catch (SQLException e) {

}

if (name.equals("?")) {

out.print(" 您没有投票,没有权利看选举结果");

} else {

if (vote) {

out.print(" 您投了一票");

// 将总票数加1:

countTotal();

// 通过连接数据库,给该候选人增加一票,

// 同时将自己的IP 地址写入数据库。

try {

rs = sql.executeQuery("SELECT * FROM people WHERE name ="

+ "'" + name + "'");

rs.next();

int count = rs.getInt("count");

count++;

String condition = "UPDATE people SET count = " + count

+ " WHERE name=" + "'" + name + "'";

// 执行更新操作(投票计数):

sql.executeUpdate(condition);

// 将IP 地址写入IP 表:

String to = "INSERT INTO IP VALUES" + "(" + "'" + IP

+ "'" + ")";

sql.executeUpdate(to);

} catch (SQLException e) {

out.print("" + e);

}

// 显示投票后的表中的记录:

try {

rs = sql.executeQuery("SELECT * FROM people");

out.print("<Table Border>");

out.print("<TR>");

out.print("<TH width=100>" + "姓名");

out.print("<TH width=50>" + "得票数");

out.print("<TH width=50>" + "总票数:" + total);

out.print("</TR>");

while (rs.next()) {

out.print("<TR>");

out.print("<TD >" + rs.getString(1) + "</TD>");

int count = rs.getInt(2);

out.print("<TD >" + count + "</TD>");

double b = (count * 100) / total; // 得票的百分比。

out.print("<TD >" + b + "%" + "</TD>");

out.print("</TR>");

}

out.print("</Table>");

con.close();

} catch (SQLException e) {

}

} else {

out.print(" 您已经投过票了");

}

}

%>

</BODY>

</HTML>

猜你喜欢