-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertStudent.java
123 lines (91 loc) · 3.57 KB
/
InsertStudent.java
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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class InsertStudent extends JFrame implements ActionListener{
JTextField studentNameTextField,studentBranchTextField,studentMobileTextField,studentGenderTextField;
JButton insertButton,backToHome;
public InsertStudent() {
// TODO Auto-generated constructor stub
initializeGUI();
}
public void initializeGUI() {
//set frame attributes
setTitle("Insert Record");
setSize(Login.WIDTH, Login.HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
JLabel insertStudentTitle = new JLabel("Insert a new student");
insertStudentTitle.setBounds(150, 50, 200, 30);
add(insertStudentTitle);
JLabel studentNameLabel = new JLabel("Student Name :");
studentNameLabel.setBounds(50, 100, 200, 30);
add(studentNameLabel);
studentNameTextField = new JTextField();
studentNameTextField.setBounds(150, 100,200,30);
add(studentNameTextField);
JLabel studentBranchLabel = new JLabel("Branch :");
studentBranchLabel.setBounds(50, 150, 200, 30);
add(studentBranchLabel);
studentBranchTextField = new JTextField();
studentBranchTextField.setBounds(150, 150,200,30);
add(studentBranchTextField);
JLabel studentMobileLabel = new JLabel("Mobile :");
studentMobileLabel.setBounds(50, 200, 200, 30);
add(studentMobileLabel);
studentMobileTextField = new JTextField();
studentMobileTextField.setBounds(150, 200,200,30);
add(studentMobileTextField);
JLabel studentGenderLabel = new JLabel("Gender :");
studentGenderLabel.setBounds(50, 250, 200, 30);
add(studentGenderLabel);
studentGenderTextField = new JTextField();
studentGenderTextField.setBounds(150, 250,200,30);
add(studentGenderTextField);
insertButton = new JButton("Insert into database");
insertButton.setBounds(150, 300, 200, 30);
insertButton.addActionListener(this);
add(insertButton);
backToHome = new JButton("Dashboard");
backToHome.setBounds(150, 350, 200, 30);
backToHome.addActionListener(this);
add(backToHome);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//insert the student into database
if(e.getSource() == insertButton) {
try {
Connection connection = DatabaseProvider.initiateConnection();
String insertQuery = "INSERT INTO students (student_name, branch, mobile, gender) VALUES (?,?,?,?);";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, studentNameTextField.getText().toString());
preparedStatement.setString(2, studentBranchTextField.getText().toString());
preparedStatement.setString(3, studentMobileTextField.getText().toString());
preparedStatement.setString(4, studentGenderTextField.getText().toString());
int rowsInserted = preparedStatement.executeUpdate();
if(rowsInserted > 0) {
System.out.println("student inserted successfully");
}
else {
System.out.println("unable to insert given student data !");
}
}catch(SQLException se) {
System.out.println("SQLException" + se.getCause());
se.printStackTrace();
}
}
if(e.getSource() == backToHome) {
dispose();
new Dashboard();
}
}
}