-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.java
More file actions
103 lines (92 loc) · 2.89 KB
/
Copy pathadd.java
File metadata and controls
103 lines (92 loc) · 2.89 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class add extends JFrame{
Container c;
JLabel labRno, labName, labCourse, labMarks;
JTextField txtRno, txtName, txtCourse, txtMarks;
JButton btnSave,btnBack;
add(){
c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
labRno = new JLabel("Enter Rno : ");
txtRno = new JTextField(10);
labName = new JLabel("Enter Name : ");
txtName = new JTextField(10);
labCourse = new JLabel("Enter Course : ");
txtCourse = new JTextField(10);
labMarks = new JLabel("Enter Marks : ");
txtMarks = new JTextField(10);
btnSave = new JButton("Add Student");
btnBack = new JButton("Back to Main");
Font f = new Font("Times New Roman",Font.BOLD,25);
labRno.setFont(f);
txtRno.setFont(f);
labName.setFont(f);
txtName.setFont(f);
labCourse.setFont(f);
txtCourse.setFont(f);
labMarks.setFont(f);
txtMarks.setFont(f);
btnSave.setFont(f);
btnBack.setFont(f);
c.add(labRno);
c.add(txtRno);
c.add(labName);
c.add(txtName);
c.add(labCourse);
c.add(txtCourse);
c.add(labMarks);
c.add(txtMarks);
c.add(btnSave);
c.add(btnBack);
class Eh1 implements ActionListener{
public void actionPerformed(ActionEvent ae){
dashboard a = new dashboard();
dispose();
}
}
btnBack.addActionListener(new Eh1());
class Eh2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
int rno = Integer.parseInt(txtRno.getText());
String name = txtName.getText();
int marks = Integer.parseInt(txtMarks.getText());
String course = txtCourse.getText();
String user = System.getenv("DB_USER");
String pwd = System.getenv("DB_PASSWORD");
String url = "jdbc:mysql://localhost:3306/student_db";
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
try(Connection con = DriverManager.getConnection(url,user,pwd);
PreparedStatement pst = con.prepareStatement("INSERT INTO student (name, rno, course, marks) VALUES (?, ?, ?, ?)")){
pst.setString(1,name);
pst.setInt(2,rno);
pst.setString(3, course);
pst.setInt(4, marks);
pst.executeUpdate();
JOptionPane.showMessageDialog(c,"Student Saved");
txtRno.setText("");
txtName.setText("");
txtCourse.setText("");
txtMarks.setText("");
txtRno.requestFocus();
} catch(SQLException e){
String msg = "issue " + e;
JOptionPane.showMessageDialog(c,msg);
}
} catch(SQLException e){
String msg = "issue " + e;
JOptionPane.showMessageDialog(c,msg);
}
}
}
btnSave.addActionListener(new Eh2());
setSize(500,350);
setLocationRelativeTo(null);
setTitle("Add Student Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}