A Java-based Online Voting System built with Core Java, JDBC, and MySQL to manage voter registration, authentication, candidate management, vote submission, and election result tracking.
The project demonstrates practical implementation of Java programming, object-oriented concepts, database connectivity, SQL queries, and CRUD operations.
The Online Voting System is a console-based application designed to provide a structured digital voting process.
It allows voters to register, log in, view their profiles, and cast their votes for available candidates. An administrator can log in separately to add candidates and view election results.
The application uses Java Database Connectivity (JDBC) to communicate with a MySQL database and store voter information, candidate details, and voting records.
- Voter Registration: Register voters with personal and identification details.
- Input Validation: Validate voter ID, Aadhaar number, phone number, email, and password format.
- Voter Authentication: Log in using registered email and password.
- Cast Vote: View available candidates and submit a vote.
- Duplicate Voting Prevention: Check whether a voter has already voted.
- View Profile: Retrieve registered voter information.
- Admin Authentication: Separate administrator login.
- Add Candidates: Register candidates with their names and political party details.
- View Election Results: Display candidate-wise vote counts.
- Candidate Management: Store and retrieve candidate information using MySQL.
- MySQL database integration using JDBC.
- Prepared statements for parameterized SQL queries.
- Relational data storage for voters, candidates, administrators, and votes.
- SQL JOIN and aggregate functions for election result calculation.
| Technology | Purpose |
|---|---|
| Java | Application development |
| JDBC | Database connectivity |
| MySQL | Relational database |
| MySQL Connector/J | JDBC driver |
| IntelliJ IDEA | Development environment |
- Object-Oriented Programming (OOP)
- Classes and methods
- Conditional statements and switch expressions
- Loops and exception handling
- Scanner for user input
- Regular expressions for input validation
- SQL INSERT, SELECT, and UPDATE
- PreparedStatement
- ResultSet
- INNER and LEFT JOIN concepts
- COUNT() and GROUP BY
- Database constraints
OnlineVotingSystem/
โ
โโโ src/
โ โโโ Main.java
โ
โโโ .idea/
โ
โโโ .gitignore
โ
โโโ OnlineVotingSystem.iml
Before running the application, make sure you have installed:
- Java JDK 14 or later
- MySQL Server 8.0 or compatible version
- MySQL Connector/J
- IntelliJ IDEA or another Java IDE
git clone https://github.com/ItsHardikSoni/OnlineVotingSystem.gitcd OnlineVotingSystemOpen MySQL Workbench or your MySQL terminal and execute:
CREATE DATABASE voting_system;
USE voting_system;Voters Table
CREATE TABLE voters (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
number VARCHAR(15) NOT NULL,
state VARCHAR(100),
aadhaar VARCHAR(12) NOT NULL,
password VARCHAR(255) NOT NULL,
voter_id VARCHAR(10) UNIQUE NOT NULL,
has_voted BOOLEAN DEFAULT FALSE
);Candidates Table
CREATE TABLE candidates (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
party VARCHAR(100) NOT NULL
);Votes Table
CREATE TABLE votes (
id INT AUTO_INCREMENT PRIMARY KEY,
voter_id INT NOT NULL,
candidate_id INT NOT NULL,
FOREIGN KEY (voter_id) REFERENCES voters(id),
FOREIGN KEY (candidate_id) REFERENCES candidates(id)
);Admin Table
CREATE TABLE admin (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);INSERT INTO admin (username, password)
VALUES ('admin', 'CHANGE_THIS_PASSWORD');Replace the example password with your own development password.
Open src/Main.java and locate the database configuration:
static final String DB_URL =
"jdbc:mysql://127.0.0.1:3306/voting_system";
static final String USER = "root";
static final String PASS = "YOUR_MYSQL_PASSWORD";Update the username and password according to your local MySQL configuration.
Important: Never commit real database credentials to a public repository. Use environment variables or an external configuration file.
Download MySQL Connector/J:
https://dev.mysql.com/downloads/connector/j/
Add the downloaded JAR file to your project's dependencies or classpath.
Open Main.java in IntelliJ IDEA and execute the main() method.
When the application starts, the following menu appears:
================================
ONLINE VOTING SYSTEM
================================
1. Voter Registration
2. Voter Login & Vote
3. View Profile
4. Admin Login
5. Exit
Choose an option:
Start Application
|
v
Voter Registration
|
v
Enter Personal Details
|
v
Input Validation
|
v
Save Voter Information
|
v
Voter Login
|
v
View Available Candidates
|
v
Cast Vote
|
v
Update Voting Status
Admin Login
|
v
Validate Credentials
|
v
Admin Dashboard
|
+----------------------+
| |
v v
Add Candidate View Results
| |
v v
Save Candidate Count Votes
| |
v v
Return to Menu Display Results
The application performs several database operations using JDBC.
| Operation | SQL Concept |
|---|---|
| Voter registration | INSERT |
| Voter authentication | SELECT |
| View voter profile | SELECT |
| Cast vote | INSERT |
| Update voting status | UPDATE |
| Add candidate | INSERT |
| Calculate election results | LEFT JOIN, COUNT(), GROUP BY |
The following query retrieves the number of votes received by each candidate:
SELECT
candidates.name,
candidates.party,
COUNT(votes.id) AS vote_count
FROM candidates
LEFT JOIN votes
ON candidates.id = votes.candidate_id
GROUP BY candidates.id;This project is an educational implementation demonstrating database-driven voting workflows. It is not a production-ready electronic voting platform.
Some important improvements for a production system include:
- Secure password hashing instead of storing plaintext passwords.
- Environment-based database credential management.
- Database transactions for atomic vote submission.
- Stronger authentication and authorization.
- Protection against concurrent duplicate voting.
- Privacy protection for sensitive voter information.
- Independent security auditing and election integrity verification.
Note: The current implementation uses basic validation and a voter status flag. These mechanisms alone do not provide the security guarantees required for real-world elections.
- Develop a graphical user interface using Java Swing or JavaFX.
- Implement secure password hashing.
- Add database transaction management.
- Introduce candidate search and filtering.
- Generate downloadable election result reports.
- Improve admin and voter authorization.
- Add comprehensive validation and exception handling.
Through this project, I practiced:
- Applying Core Java and OOP concepts.
- Connecting Java applications with MySQL using JDBC.
- Writing SQL queries for relational databases.
- Implementing authentication and registration workflows.
- Working with PreparedStatement and ResultSet.
- Handling database exceptions and user input validation.
- Building a complete database-driven console application.
Ayush (Hardik Soni)
Software Developer | Java | Full-Stack & Mobile Application Development
- Portfolio: https://developerayush.tech
- GitHub: https://github.com/ItsHardikSoni
- LinkedIn: https://www.linkedin.com/in/imhardiksoni/
- Instagram: https://www.instagram.com/its.hardiksoni/
- X: https://x.com/imHardikSoni
This project is available for educational and learning purposes. Please check the repository's license information before reusing or distributing the code.
Built with Java and MySQL | Learning by Building ๐