Skip to content

Commit 8528255

Browse files
Merge pull request #416 from vidyaa18/vidya
Add Online voting system using java servlets
2 parents 27982b2 + 143b208 commit 8528255

File tree

22 files changed

+1296
-0
lines changed

22 files changed

+1296
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package vote.com.servlet;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
7+
public class DBUtilR {
8+
static Connection conn = null;
9+
static
10+
{
11+
try {
12+
Class.forName("com.mysql.jdbc.Driver");
13+
14+
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/votingdb", "Vaishnavi", "Nelavetla@537");
15+
16+
if(!conn.isClosed()) {
17+
System.out.println("Connection established");
18+
}
19+
20+
} catch (ClassNotFoundException | SQLException e) {
21+
System.out.println("Error in DBUtilFile");
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
public static Connection getDBConnection() {
27+
// TODO Auto-generated method stub
28+
return conn;
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package vote.com.servlet;
2+
3+
import java.io.IOException;
4+
5+
import jakarta.servlet.ServletException;
6+
import jakarta.servlet.http.HttpServlet;
7+
import jakarta.servlet.http.HttpServletRequest;
8+
import jakarta.servlet.http.HttpServletResponse;
9+
10+
public class againvote extends HttpServlet {
11+
private static final long serialVersionUID = 1L;
12+
13+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
14+
{
15+
doGet(request, response);
16+
}
17+
18+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package vote.com.servlet;
2+
3+
import jakarta.servlet.RequestDispatcher;
4+
import jakarta.servlet.ServletException;
5+
import jakarta.servlet.http.HttpServlet;
6+
import jakarta.servlet.http.HttpServletRequest;
7+
import jakarta.servlet.http.HttpServletResponse;
8+
9+
import java.io.IOException;
10+
import java.io.PrintWriter;
11+
import java.sql.Connection;
12+
import java.sql.PreparedStatement;
13+
import java.sql.ResultSet;
14+
import java.sql.SQLException;
15+
16+
public class loginpage extends HttpServlet {
17+
private static final long serialVersionUID = 1L;
18+
19+
final static Connection con=DBUtilR.getDBConnection();
20+
static PreparedStatement ps = null;
21+
22+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
23+
{
24+
PrintWriter out = response.getWriter();
25+
String card=request.getParameter("cardno");
26+
Integer pin=Integer.parseInt(request.getParameter("pin"));
27+
28+
try {
29+
if(check(card,pin))
30+
{
31+
out.print("Successful Login...You Can Vote Now");
32+
RequestDispatcher rd=request.getRequestDispatcher("vote.html");
33+
rd.include(request,response);
34+
}
35+
else {
36+
out.print("Sorry username or password error , Make new account");
37+
RequestDispatcher rd=request.getRequestDispatcher("registration.html");
38+
rd.include(request,response);
39+
}
40+
}
41+
catch (SQLException e) {
42+
e.printStackTrace();
43+
}
44+
45+
46+
}
47+
static boolean check(String card,Integer pin) throws SQLException
48+
{
49+
boolean r=false;
50+
ps=con.prepareStatement("Select * from register where cardno=? and pin=?");
51+
ps.setString(1,card);
52+
ps.setInt(2,pin);
53+
ResultSet rs=ps.executeQuery();
54+
r=rs.next();
55+
56+
return r;
57+
}
58+
59+
static boolean checkvote(String card) throws SQLException
60+
{
61+
boolean r=false;
62+
ps=con.prepareStatement("Select * from vote where cardno=?");
63+
ps.setString(1,card);
64+
65+
ResultSet rs=ps.executeQuery();
66+
r=rs.next();
67+
68+
return r;
69+
}
70+
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package vote.com.servlet;
2+
3+
import jakarta.servlet.RequestDispatcher;
4+
import jakarta.servlet.ServletException;
5+
import jakarta.servlet.http.HttpServlet;
6+
import jakarta.servlet.http.HttpServletRequest;
7+
import jakarta.servlet.http.HttpServletResponse;
8+
9+
import java.io.IOException;
10+
import java.io.PrintWriter;
11+
import java.sql.Connection;
12+
import java.sql.DriverManager;
13+
import java.sql.PreparedStatement;
14+
15+
public class registration extends HttpServlet {
16+
private static final long serialVersionUID = 1L;
17+
18+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
19+
{
20+
response.setContentType("text/html");
21+
PrintWriter out = response.getWriter();
22+
23+
24+
String f=request.getParameter("fname");
25+
26+
String c=request.getParameter("cardno");
27+
String cn=request.getParameter("cono");
28+
String ad=request.getParameter("add");
29+
String dob=request.getParameter("dob");
30+
String email=request.getParameter("email");
31+
String pin=request.getParameter("pin");
32+
try
33+
{
34+
35+
Class.forName("com.mysql.jdbc.Driver");
36+
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/votingdb","Vaishnavi","Nelavetla@537");
37+
PreparedStatement ps=con.prepareStatement("insert into register values(?,?,?,?,?,?,?)");
38+
ps.setString(1,f);
39+
40+
ps.setString(2,c);
41+
ps.setString(3,cn);
42+
ps.setString(4,ad);
43+
ps.setString(5,dob);
44+
ps.setString(6,email);
45+
ps.setString(7,pin);
46+
int i=ps.executeUpdate();
47+
if(i>0)
48+
{
49+
out.print("Successfully your account has been created...PLEASE LOGIN");
50+
RequestDispatcher rd=request.getRequestDispatcher("loginpage.html");
51+
rd.include(request,response);
52+
}
53+
else
54+
{
55+
out.print("Failed account creation try again");
56+
RequestDispatcher rd=request.getRequestDispatcher("registration.html");
57+
rd.include(request,response);
58+
}
59+
60+
}
61+
catch (Exception e2) {
62+
out.print("Invalid , Failed account creation try again "+e2);
63+
RequestDispatcher rd=request.getRequestDispatcher("registration.html");
64+
rd.include(request,response);
65+
}
66+
67+
out.close();
68+
69+
}
70+
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
71+
{
72+
doPost(request, response);
73+
}
74+
75+
}

0 commit comments

Comments
 (0)