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
很奇怪
请高手帮忙.

解决方案 »

  1.   

    test=="A"我认为是这个的问题使用test.equals("A")试下
      

  2.   

    用IF---ELSE IF---ELSE来实现试试
      

  3.   

    最终解决方案
    if(test.equals("A"))
    { stm.execute("update test_info set test1=test1+1 where id=1"); }
    else if(test.equals("B"))
    { stm.execute("update test_info set test2=test2+1 where id=1"); } else if(test.equals("C"))
    { stm.execute("update test_info set test3=test3+1 where id=1"); }
    else if(test.equals("D"))
    { stm.execute("update test_info set test4=test4+1 where id=1"); }还不知道原因是为什么
    .....高手来讨论下...晚上结帖
      

  4.   

    test=="A" ?????---->
    test.equals("A")
    建议看一下java的基础知识,区分一下=和equals的不同
      

  5.   

    你大意了,
    if(test=="C");
    if(test=="D");
    因为这两句后面都有分号,下面的语句当然总执行了。
      

  6.   

    字符串相等通常只用equals,==这个是两个对象完全相等才用,但你这个不知道为什么即可等于"C"又可等于“D”
      

  7.   

    java基础没学好哈...最近学校排了很多基础语言课
    C# JAVA C 同学期同时进行,脑子乱的很,继续努力.
    原因 已查明....我>>>>>犯混了.