-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewBooks.java
119 lines (87 loc) · 3.06 KB
/
ViewBooks.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
package BookClasses;
import AdminClasses.AdminLoged;
import DbClasses.DB;
import UserClasses.UserLoged;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class ViewBooks extends JFrame {
public JTable table;
static JFrame frame;
public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable(){
public void run()
{
try{
frame = new ViewBooks();
frame.setVisible(true);
}catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public ViewBooks() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(180, 80, 990, 550);
setTitle("Library Book Database");
String[][] data = null;
String[] column = null;
try {
Connection con = DB.getConnection();
PreparedStatement ps = con.prepareStatement("select * from books", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
column = new String[cols];
for (int i = 1; i <= cols; i++) {
column[i - 1] = rsmd.getColumnName(i);
}
if (!rs.next()) {
System.out.println("No data in the result set.");
} else {
rs.last();
int rows = rs.getRow();
rs.beforeFirst();
data = new String[rows][cols];
int count = 0;
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
data[count][i - 1] = rs.getString(i);
}
count++;
}
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
JButton btnback = new JButton("Back");
btnback.setBounds(750, 468, 120, 40);
btnback.setBackground(Color.GRAY);
add(btnback);
btnback.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
UserLoged.main(new String[]{});
frame.dispose();
}
});
if (column != null && data != null) {
table = new JTable(data, column);
table.setBackground(Color.yellow);
JScrollPane sp = new JScrollPane(table);
add(sp);
} else {
// Handle the case where columnNames is null (result set is empty)
System.out.println("No columns to display.");
}
}
}