-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewLibraryBooks.java
More file actions
181 lines (158 loc) · 6.43 KB
/
ViewLibraryBooks.java
File metadata and controls
181 lines (158 loc) · 6.43 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
package net.javaguides.swing;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
public class ViewLibraryBooks extends JFrame {
private JPanel contentPane;
private JTable table;
private JButton btnBack;
private JScrollPane scrollPane;
private JTextField txtSearch;
private JButton btnSearch;
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/swing_demo";
static final String USER = "root";
static final String PASS = "Aben5099";
public ViewLibraryBooks() {
initializeUI();
populateTable();
}
private void initializeUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUpdateBooks = new JLabel("View Books");
lblUpdateBooks.setForeground(Color.BLACK);
lblUpdateBooks.setFont(new Font("Times New Roman", Font.PLAIN, 46));
lblUpdateBooks.setBounds(363, 52, 333, 93);
contentPane.add(lblUpdateBooks);
// Create a panel for search functionality
JPanel searchPanel = new JPanel();
JLabel lblSearch = new JLabel("Search:");
txtSearch = new JTextField(20);
btnSearch = new JButton("Search");
// Add search button action listener
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String searchText = txtSearch.getText().trim();
filterTable(searchText);
}
});
// Add components to the search panel
searchPanel.add(lblSearch);
searchPanel.add(txtSearch);
searchPanel.add(btnSearch);
searchPanel.setBounds(70, 150, 350, 30);
contentPane.add(searchPanel);
scrollPane = new JScrollPane();
scrollPane.setBounds(70, 200, 850, 250);
contentPane.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
btnBack = new JButton("Back");
btnBack.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnBack.setBounds(50, 470, 218, 73);
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
AdminHome ah = new AdminHome();
ah.setTitle("Admin Home");
ah.setVisible(true);
}
});
contentPane.add(btnBack);
JLabel label = new JLabel("");
label.setBounds(0, 0, 1008, 562);
contentPane.add(label);
}
private void populateTable() {
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
String sql = "SELECT name, book_title, book_id, time_borrowed, time_returned FROM fees_management";
ResultSet rs = stmt.executeQuery(sql);
DefaultTableModel model = new DefaultTableModel(new Object[][] {},
new String[] { "Name", "Book Title", "Book ID", "Time Borrowed", "Time Returned" });
while (rs.next()) {
String name = rs.getString("name");
if (name.equalsIgnoreCase("library") || name.equalsIgnoreCase("LIBRARY")
|| name.equalsIgnoreCase("Library")) {
name = "Library";
} else {
name = "Booked";
}
model.addRow(new Object[] { name, rs.getString("book_title"), rs.getInt("book_id"),
rs.getTimestamp("time_borrowed"), rs.getTimestamp("time_returned") });
}
table.setModel(model);
rs.close();
stmt.close();
conn.close();
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
private void filterTable(String searchText) {
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
String sql = "SELECT name, book_title, book_id, time_borrowed, time_returned FROM fees_management WHERE name LIKE '%"
+ searchText + "%' OR book_title LIKE '%" + searchText + "%'";
ResultSet rs = stmt.executeQuery(sql);
DefaultTableModel model = new DefaultTableModel(new Object[][] {},
new String[] { "Name", "Book Title", "Book ID", "Time Borrowed", "Time Returned" });
while (rs.next()) {
String name = rs.getString("name");
if (name.equalsIgnoreCase("library") || name.equalsIgnoreCase("LIBRARY")
|| name.equalsIgnoreCase("Library")) {
name = "Library";
} else {
name = "Booked";
}
model.addRow(new Object[] { name, rs.getString("book_title"), rs.getInt("book_id"),
rs.getTimestamp("time_borrowed"), rs.getTimestamp("time_returned") });
}
table.setModel(model);
rs.close();
stmt.close();
conn.close();
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
ViewLibraryBooks frame = new ViewLibraryBooks();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}