-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddProject.java
More file actions
162 lines (154 loc) · 6.27 KB
/
AddProject.java
File metadata and controls
162 lines (154 loc) · 6.27 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
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.*;
import java.io.*;
import java.util.*;
public class AddProject{
JFrame projFrame;
JPanel s1,s2,s3;
JTabbedPane tab;
JLabel id,proj_id,dept_id,proj_name,del_id;
JTextField id_text,proj_text,dept_text,proj_name_text,del_id_text;
JButton addbtn,clearbtn,viewbtn,delbtn;
JTable projtable;
JComboBox projName;
AddProject(){
projFrame = new JFrame();
projFrame.setSize(500,400);
s1 = new JPanel(new GridLayout(5,2));
s2 = new JPanel();
s3 = new JPanel();
tab = new JTabbedPane(JTabbedPane.LEFT,JTabbedPane.WRAP_TAB_LAYOUT);
id = new JLabel("Employee ID");
dept_id = new JLabel("Department ID");
proj_id = new JLabel("Project ID");
proj_name = new JLabel("Project Name");
String[] names = {"C++","Java","Python","System Design","Flutter","C#","Go Lang","DataBase","FrontEnd","Rust","Rest API"};
projName = new JComboBox(names);
del_id = new JLabel("Emp_ID to Delete");
del_id.setBounds(0,2,301,59);
id_text = new JTextField();
dept_text = new JTextField();
proj_text = new JTextField();
// proj_name_text = new JTextField();
del_id_text = new JTextField();
del_id_text.setBounds(131,14,170,36);
addbtn = new JButton("Add");
clearbtn = new JButton("Clear");
viewbtn = new JButton("View");
delbtn = new JButton("Delete");
delbtn.setBounds(57,71,170,47);
viewbtn.setBounds(49,41,178,62);
s1.add(id);
s1.add(id_text);
s1.add(dept_id);
s1.add(dept_text);
s1.add(proj_id);
s1.add(proj_text);
s1.add(proj_name);
s1.add(projName);
s1.add(addbtn);
s1.add(clearbtn);
s2.setLayout(null);
s2.add(viewbtn);
s3.setLayout(null);
s3.add(del_id);
s3.add(del_id_text);
s3.add(delbtn);
addbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String emp_id = id_text.getText();
String dept_id = dept_text.getText();
String proj_id = proj_text.getText();
// String proj_name = proj_name_text.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "sys";
String driver = "com.mysql.cj.jdbc.Driver";
String user = "***";
String pass = "***";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("INSERT INTO project (id,proj_id,dept_id,proj_name) VALUES (?, ?, ?, ?)");
st.setString(1, emp_id);
st.setString(2, proj_id);
st.setString(3, dept_id);
st.setString(4, (String) projName.getSelectedItem());
st.executeUpdate();
JOptionPane.showMessageDialog(null, "Project Added Successfully");
con.close();
}
catch(Exception ex){
System.out.println(ex);
JOptionPane.showMessageDialog(null, "Error Occured");
}
}
});
clearbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
id_text.setText("");
dept_text.setText("");
proj_text.setText("");
proj_name_text.setText("");
}
});
viewbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
projtable = new JTable();
JFrame viewProj = new JFrame("View Projects");
viewProj.setSize(600,400);
DefaultTableModel projModel = new DefaultTableModel();
projModel.setColumnIdentifiers(new String[]{"ID","Project ID","Department ID","Project Name"});
projtable.setModel(projModel);
viewProj.getContentPane().add(new JScrollPane(projtable));
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sys","***","***");
PreparedStatement ps = con.prepareStatement("select * from project");
ResultSet rs = ps.executeQuery();
while(rs.next()){
projModel.addRow(new Object[]{rs.getString("id"),rs.getString("proj_id"),rs.getString("dept_id"),rs.getString("proj_name")});
}
}
catch(Exception ex){
System.out.println(ex);
}
viewProj.setVisible(true);
}
});
delbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sys","***","***");
PreparedStatement ps = con.prepareStatement("delete from project where id = ?");
ps.setString(1,del_id_text.getText());
System.out.println(del_id_text.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Data Deleted");
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
}
void Dis(){
projFrame.getContentPane().add(tab);
tab.addTab("Add Project",s1);
tab.addTab("View Project",s2);
tab.addTab("Delete Project",s3);
projFrame.setSize(400,400);
projFrame.setVisible(true);
projFrame.setResizable(true);
}
public static void main(String[] args) {
AddProject add = new AddProject();
add.Dis();
}
}