-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategory.java
More file actions
207 lines (172 loc) · 5.47 KB
/
Category.java
File metadata and controls
207 lines (172 loc) · 5.47 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
package SLibrary;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JTextField;
import net.proteanit.sql.DbUtils;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
public class Category {
JFrame Categoryy;
private JTextField txtcategory;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Category window = new Category();
window.Categoryy.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// static boolean isVisible = true;
/**
* Create the application.
*/
public Category() {
initialize();
Connect();
Category_load() ;
//table_load();
}
Connection con;
PreparedStatement pst;
ResultSet rs;
private JTable table_1;
//private JTable table_3;
public void Connect()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/SLibrary","root","");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public void Category_load() {
int c;
try {
String query="select * from Category";
pst=con.prepareStatement(query);
rs = pst.executeQuery();
//// ResultSetMetaData rsd=rs.getMetaData();
// c=rsd.getColumnCount();
//// DefaultTableModel d=(DefaultTableModel)jTablel.getModel();
// pst=con.prepareStatement("select * from Category");
// rs=pst.executeQuery();
table_1.setModel(DbUtils.resultSetToTableModel(rs));
//30
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Categoryy = new JFrame();
Categoryy.setBounds(100, 100, 949, 522);
Categoryy.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Categoryy.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(44, 49, 826, 394);
Categoryy.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Category");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 19));
lblNewLabel.setBounds(126, 41, 129, 37);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Category Name");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1.setBounds(21, 107, 137, 28);
panel.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("Status");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel_1_1.setBounds(21, 145, 137, 28);
panel.add(lblNewLabel_1_1);
txtcategory = new JTextField();
txtcategory.setBounds(177, 114, 96, 19);
panel.add(txtcategory);
txtcategory.setColumns(10);
JComboBox txtstatus = new JComboBox();
txtstatus.setModel(new DefaultComboBoxModel(new String[] {"Active", "DeActive"}));
txtstatus.setBounds(177, 152, 96, 21);
panel.add(txtstatus);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String category=txtcategory.getText();
String status=txtstatus.getSelectedItem().toString();
try {
pst=con.prepareStatement("insert into Category(catname,status)values(?,?)");
pst.setString(1, category);
pst.setString(2, status);
int k=pst.executeUpdate();
if(k==1) {
JOptionPane.showMessageDialog(null, "Category created");
Category_load();
txtcategory.setText("");
txtstatus.setSelectedIndex(-1);
txtcategory.requestFocus();
}
else {
JOptionPane.showMessageDialog(null, "Error!!!!!!!!");
}
}catch(SQLException ex) {
ex.printStackTrace();
}
}
});
btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnAdd.setBounds(56, 250, 85, 21);
panel.add(btnAdd);
JButton btnUpdate = new JButton("Update");
btnUpdate.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnUpdate.setBounds(177, 250, 85, 21);
panel.add(btnUpdate);
JButton btnDelete = new JButton("Delete");
btnDelete.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnDelete.setBounds(56, 286, 85, 21);
panel.add(btnDelete);
JButton btnCancel = new JButton("Cancel");
btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnCancel.setBounds(177, 286, 85, 21);
panel.add(btnCancel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 37, 388, 315);
panel.add(scrollPane);
// table_3 = new JTable();
// scrollPane.setViewportView(table_3);
JScrollPane table_2 = new JScrollPane();
//scrollPane.setViewportView(table);
table_2.setFont(new Font("Tahoma", Font.PLAIN, 10));
table_1 = new JTable();
table_2.setViewportView(table_1);
}
}