日期:2014-05-18  浏览次数:20819 次

一个鬼问题使用Servlet制作投票程序,要将结果写入数据库.高手来看看
package   com;
import   javax.servlet.*;
import   javax.servlet.http.*;
import   java.sql.*;
import   com.microsoft.jdbc.sqlserver.SQLServerDriver;
import   java.util.*;
import   java.io.*;
public   class   SendTestServlet   extends   HttpServlet
{
private   Connection   con;

public   void   doGet(HttpServletRequest   request,
HttpServletResponse   response)
throws   IOException,   ServletException
{
try
{
String   test=request.getParameter( "test ");
Statement   stm=con.createStatement();  
try
{
if(test== "A ")
{

stm.execute( "update   test_info   set   test1=test1+1   where   id=1 ");

}
if(test== "B ")
{

stm.execute( "update   test_info   set   test2=test2+1   where   id=1 ");

}

if(test== "C ");
{

stm.execute( "update   test_info   set   test3=test3+1   where   id=1 ");

}

if(test== "D ");
{

stm.execute( "update   test_info   set   test4=test4+1   where   id=1 ");

}


}
catch(Exception   e)
{
}
RequestDispatcher   requestDispatcher   =   request.getRequestDispatcher( "/viewTest_servlet ");
requestDispatcher.forward(request,response);
}
catch(Exception   e)
{
e.printStackTrace();
}
}
public   void   doPost(HttpServletRequest   request,
                        HttpServletResponse   response)
throws   IOException,   ServletException
{
doGet(request,response);
}
public       SendTestServlet()
{
String   CLASSFORNAME= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String   SERVANDDB= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=ts5 ";
String   USER= "bn ";
String   PWD= "bn ";  
try
{

Class.forName(CLASSFORNAME);
con   =   DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception   e)
{
e.printStackTrace();
}
}

}


投票页面为常规表单GET提交方法
4个单选框的名称都为test   定义4个值分别为ABCD  
数据库结构为   id   test1   test2   test3   test4
通过if语句来判断用户的选择..
但是执行后不管选的是哪一个   test3   和test4   的值都+1
很奇怪
请高手帮忙.

------解决方案--------------------
test== "A "我认为是这个的问题使用test.equals( "A ")试下
------解决方案--------------------
用IF---ELSE IF---ELSE来实现试试