Topic: jsp投票系统的一个小问题

  Print this page

1.jsp投票系统的一个小问题 Copy to clipboard
Posted by: kkk
Posted on: 2006-06-22 15:09

jsp投票系统的一个小问题.<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<body>
<%!
int total=0;
synchronized void countTotal()
{
total++;
}
%>
在这里定义了一个函数它自动加1来表示,总共参加投票的人数.
<%
boolean vote=true;
String name="";
name=request.getParameter("name");
if(name==null)
{ name="?";
}
byte a[]=name.getBytes("ISO-8859-1");
name =new StringAngel;
String IP=(String)request.getRemoteAddr();
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e){}
Connection con=null;
Statement sql=null;
ResultSet rs=null;
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;SelectMethod=cursor","sa", "123456");
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("您投了一票");
countTotal(); //这里它只能自加1一次,以后就再也加不了了.
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);
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); //在这里,它的值就一直是1了.没有变过.
out.print("</TR>");
while(rs.next())
{
out.print("<TR>");
out.print("<TD >"+rs.getString(1)+"</TD>");
int count=rs.getInt("count");
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>

我就是想问问,为什么它的值老是1呀,不会再加了呢??这是为什么呀???

各位大侠,能不能帮我看一下呀.谢谢了呀.

2.Re:jsp投票系统的一个小问题 [Re: kkk] Copy to clipboard
Posted by: chengbd
Posted on: 2006-06-22 18:18

代码太长,发个完整的项目吧


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923