Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this a voter will first login and then a jsp page will be displayed in front of him which will display the names of all candidates after retrieving it from database.When the voter will select one candidate name and click the submit button it shoul be redirected to a servlet whose work is to increade the no_of_votes of that particular candidate and save it into the database and make the boolean value true into the database which will indicate that the voter has voted and cant vote again.so I have written the jsp and servlet codes n attaching here..but its not working..please help me and tell me what is the error here.

jsp code

ASP.NET
<%@ page import="p1.*" session="true"%>

<html>
<head>
<title>My Page</title>
</head>
<body BGCOLOR="GREEN">

<H1><center>CAST      YOUR      VOTE</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>



Servlet Code

Java
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)
{ 
}
}                    	
Posted
Updated 2-Apr-12 20:30pm
v2
Comments
Darkness_07 18-May-12 6:35am    
I don't know if you forgot to put a SERVLETNAME in your form action
leejoyprakash 21-Sep-12 10:35am    
Did you configure and test your ODBC DataSource ? Can you print and check whether any exception is raised in both servlet and JSP ?
catch(Exception e){ e.printStackTrace();} .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900