This repository was archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnline_voting.java
More file actions
72 lines (57 loc) · 2.44 KB
/
Online_voting.java
File metadata and controls
72 lines (57 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class OnlineVotingSystem extends Application {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/online_voting_system";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Online Voting System");
GridPane grid = new GridPane();
grid.setPadding(new Insets(20, 20, 20, 20));
grid.setVgap(8);
grid.setHgap(10);
Label nameLabel = new Label("Candidate Name:");
GridPane.setConstraints(nameLabel, 0, 0);
TextField nameInput = new TextField();
GridPane.setConstraints(nameInput, 1, 0);
Button voteButton = new Button("Vote");
GridPane.setConstraints(voteButton, 1, 1);
voteButton.setOnAction(e -> voteCandidate(nameInput.getText()));
grid.getChildren().addAll(nameLabel, nameInput, voteButton);
Scene scene = new Scene(grid, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
private void voteCandidate(String candidateName) {
try (Connection connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD)) {
String sql = "INSERT INTO votes (candidate_name) VALUES (?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, candidateName);
preparedStatement.executeUpdate();
showAlert("Vote Successful", "Thank you for voting!");
}
} catch (SQLException e) {
showAlert("Error", "Failed to vote. Please try again.");
e.printStackTrace();
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}