-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
231 lines (193 loc) · 8.1 KB
/
Main.java
File metadata and controls
231 lines (193 loc) · 8.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package StudentDatabase.gitRepo.StudentDatabaseSystem;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
static JFrame f;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int Screenwidth = (int) screenSize.getWidth();
int ScreenHeight = (int) screenSize.getHeight();
static boolean proceed = false;
int width = 700;
int height = 600;
ResultSet result;
boolean deleting = false;
Main(){
// this code will execute only after user authenticated itself
f = new JFrame();
f.setLayout(null);
// f.getContentPane().setBackground(new Color(254, 234, 250));
JLabel title = new JLabel("Student Database System", SwingConstants.CENTER);
title.setBounds(50, 40, 600, 80);
title.setFont(new Font("", Font.PLAIN, 30));
JPanel mainButtons = new JPanel();
mainButtons.setBounds(20, 150, 650, 350);
// mainButtons.setBackground(Color.red);
mainButtons.setLayout(null);
f.add(mainButtons);
JButton addStudentBtn = new JButton("Add new Student");
addStudentBtn.setBounds(100, 20, 200, 120);
addStudentBtn.setFont(new Font("", Font.PLAIN, 18));
addStudentBtn.setBackground(Color.WHITE);
addStudentBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// code to create a jframe that will get student data and add it to student database
new AddStudent(f.getLocation(), f.getWidth(), f.getHeight());
f.dispose();
}
});
mainButtons.add(addStudentBtn);
JButton fetchStudentBtn = new JButton("Fetch Student data");
fetchStudentBtn.setBounds(350, 20, 200, 120);
fetchStudentBtn.setFont(new Font("", Font.PLAIN, 18));
fetchStudentBtn.setBackground(Color.WHITE);
fetchStudentBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FetchData(f.getLocation(), f.getWidth(), f.getHeight());
f.dispose();
}
});
mainButtons.add(fetchStudentBtn);
JButton modifyStudentBtn = new JButton("Update Student data");
modifyStudentBtn.setBounds(100, 180, 200, 120);
modifyStudentBtn.setFont(new Font("", Font.PLAIN, 18));
modifyStudentBtn.setBackground(Color.WHITE);modifyStudentBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String ans = JOptionPane.showInputDialog("Enter roll Number : ");
if((ans != null) && (ans.trim() != "")){
try {
int rollno = Integer.parseInt(ans);
DatabaseWork(rollno);
if(result.isBeforeFirst()){
new AddStudent(f.getLocation(), f.getWidth(), f.getHeight(), result);
f.dispose();
}
else{
JOptionPane.showMessageDialog(f, "No student from provided roll no.");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(f, "Some error occurred");
}
}
}
});
mainButtons.add(modifyStudentBtn);
JButton deleteStudentBtn = new JButton("Delete Student data");
deleteStudentBtn.setBounds(350, 180, 200, 120);
deleteStudentBtn.setFont(new Font("", Font.PLAIN, 18));
deleteStudentBtn.setBackground(Color.WHITE);
deleteStudentBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String ans = JOptionPane.showInputDialog("Enter roll Number : ");
if( (ans != null) && (ans.trim() != "")){
int rollno = Integer.parseInt(ans);
deleting = true;
DatabaseWork(rollno);
deleting = false;
}
}
});
mainButtons.add(deleteStudentBtn);
f.add(title);
f.setVisible(true);
f.setSize(width, height);
f.setLocation(Screenwidth/2 - 300, ScreenHeight/2 - 350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void DatabaseWork(int rollno){
try{
Class.forName("com.mysql.jdbc.Driver");
String link = "jdbc:mysql://localhost:3308/StudentDatabase?autoReconnect=true&useSSL=false";
String username = "root";
String password = "";
com.mysql.jdbc.Connection con = (com.mysql.jdbc.Connection) DriverManager.getConnection(link, username, password);
Statement stmt = con.createStatement();
String sql = "select * from student where RollNo = " + rollno;
result = stmt.executeQuery(sql);
if(deleting){
if(result.isBeforeFirst()){
Statement statement = con.createStatement();
int done = statement.executeUpdate("delete from student where RollNo = " + rollno);
if(done > 0){
JOptionPane.showMessageDialog(f, "Student data has been deleted");
}
else{
JOptionPane.showMessageDialog(f, "Error while deleting student data");
}
}
else{
JOptionPane.showMessageDialog(f, "No student from provided roll no.");
}
}
}
catch(Exception e){
JOptionPane.showMessageDialog(f, "Error while connecting to database");
}
}
public static void authenticate(){
f = new JFrame();
f.setUndecorated(true);
f.setSize(250, 100);
f.setLocation(500, 300);
f.setLayout(null);
JLabel l = new JLabel("Enter password : ");
l.setBounds(10, 5, 100, 30);
JTextField tf = new JTextField(10);
tf.setBounds(120, 12, 100, 20);
JButton submit = new JButton("OK");
submit.setBounds(45, 50, 60, 20);
// to set focus on submit button by default
f.getRootPane().setDefaultButton(submit);
submit.requestFocus();
JButton cancel = new JButton("Cancel");
cancel.setBounds(115, 50, 80, 20);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String password = tf.getText();
if(password.equals(""))
System.exit(0);
else if(password.equals("1234")){
f.dispose();
proceed = true;
}
else{
JOptionPane.showMessageDialog(f, "invalid password");
}
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.add(l);
f.add(tf);
f.add(submit);
f.add(cancel);
f.setVisible(true);
}
public static void main(String args[]){
// First authentication : If user entered 1234 it will be given permission to proceed further
authenticate();
while(true){
System.out.print("");
if(proceed == true)
break;
}
new Main();
}
}