-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewBooks.java
More file actions
74 lines (60 loc) · 1.68 KB
/
ViewBooks.java
File metadata and controls
74 lines (60 loc) · 1.68 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
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ViewBooks {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ViewBooks window = new ViewBooks();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ViewBooks() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 640, 460);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
}
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);}
JTable table = new JTable(data,column);
JScrollPane sp=new JScrollPane(table);
frame.add(sp, BorderLayout.CENTER);
}
}