need help urgently regarding advance java project..

Discussion in 'Programming' started by Riyanath, Apr 5, 2012.

  1. #1
    Hi,
    My project is on "Online Voting System"...it has 2 users 'admin' and 'voter'.The login of voter is done.Now what needed to be done is after loging in a jsp page will be displayed in front of the voter which will show all the candidates name and an option botton with each name ,so the voter has to select 1 candidate and then click the submit button.This jsp page is redirecting to a servlet which shoul increase the number of votes of that particular candidate and it should also update the voters 'voted' attribute to '1' which means the voter has voted and can't vote again.So i have written the code(jsp,xml and servlet) and the servlet has compiled properly and even not giving any exception but its not doing its job ..that is not increasing the number of votes of the candidate n niether updating the value of the voter to '1'.When the 'submit' button is clicked then a page get opend which says "Thankyou null.you have successfully voted"..this page is being redirected from the servlet.
    please help me find out the problem.

    The jsp code

    [HTML]<%@ page import="p1.*" session="true"%>
    
    <html>
    <head>
    <title>My Page</title>
    </head>
    <body BGCOLOR="GREEN">
    
    <H1><center><B><U>CAST &nbsp&nbsp   YOUR&nbsp&nbsp    VOTE</U></B></CENTER></H1>
    <div align="center">
    <form action="#">
    <%
    String cname="";
    String cid="";
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	Connection con = DriverManager.getConnection("jdbc:odbc:zeal");
          PreparedStatement ps = con.prepareStatement("select c_name,id from candidate");
          ResultSet rs=ps.executeQuery();
    %>
    <table>
    <th>
    <td>CANDIDATES NAME</td>
    <td>VOTE</td>
    </th>
    <%
    while(rs.next())
    {
    cname=rs.getString(1);
    cid=rs.getString(2);
     out.print("<tr><td>"+cname+"</td></tr>"+"<tr><td>"+"<input type="radio" name="group1" value="cid">"+"</tr></td>");
    }
    }
    catch(Exception e)
    {
    }
    %>
    <input type="submit" name="submit">
    </div>
    
    </form>
    </body>
    </html>
    
    HTML:
    [/HTML]

    The Servlet Code

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class CastVote extends HttpServlet{
      public void doGet(HttpServletRequest request,HttpServleResponse response)throws ServletException,IOException
    {
                          cid=request.getParameter("id");
                          vid=request.getParameter("uid");
    }	
                   try
    {
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	 Connection con = DriverManager.getConnection("jdbc:odbc:zeal");
                 PreparedStatement ps=con.prepareStatement("update candidate set no_of_votes=no_of_votes+1 where id="cid");
              ResultSet rs=ps.executeUpdate();
    con.close();
    }
    catch(Exception e)
    { 
    }
    }
          
        
    	
                          
                          
                         	
    
    Code (markup):
    thankyou.
     
    Riyanath, Apr 5, 2012 IP
  2. mikeweller

    mikeweller Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #2
    There are a few things wrong with the servlet code.

    First, cid and vid are declared without types, so I don't see how this servlet would even compile. And vid isn't even used. Change these 2 lines:

    cid=request.getParameter("id");
    vid=request.getParameter("uid");

    to this:

    String cid =
    request.getParameter("id");

    The other problem is that the quotes are imbalanced in your
    PreparedStatement line.

    Change this line:

    PreparedStatement ps=con.prepareStatement("update candidate set no_of_votes=no_of_votes+1 where id="cid");

    to this:

    PreparedStatement ps=con.prepareStatement("update candidate set no_of_votes=no_of_votes+1 where id=" + cid);
     
    mikeweller, Apr 14, 2012 IP