-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayQueryResults.java
More file actions
155 lines (132 loc) · 5.3 KB
/
Copy pathDisplayQueryResults.java
File metadata and controls
155 lines (132 loc) · 5.3 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
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.regex.PatternSyntaxException;
import javax.swing.*;
import javax.swing.table.*;
public class DisplayQueryResults extends JFrame
{
private static final String DATABASE_URL="jdbc:derby:C:\\Users\\yehudit\\Documents\\מדעי המחשב - האוניברסיטה הפתוחה\\סדנה בתכנות מתקדם בשפת ג'אווה 20503\\JDBC Lecture\\students";
private static final String USERNAME="kerido";
private static final String PASSWORD="kerido";
private static final String DEFAULT_QUERY="SELECT * FROM students";
private static ResultSetTableModel tableModel;
public static void main (String [] args)
{
try
{
tableModel=new ResultSetTableModel (DATABASE_URL, USERNAME, PASSWORD, DEFAULT_QUERY);
final JTextArea queryArea=new JTextArea (DEFAULT_QUERY, 3, 100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);
JScrollPane scrollpane=new JScrollPane (queryArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JButton submitButton=new JButton("submit Query");
Box boxNorth=Box.createHorizontalBox();
boxNorth.add(scrollpane);
boxNorth.add(submitButton);
JTable resultTable=new JTable (tableModel);
JLabel filterLabel=new JLabel("Filter");
final JTextField filterText = new JTextField();
JButton filterButton = new JButton("Apply Filter");
Box boxSouth = Box.createHorizontalBox();
boxSouth.add(filterLabel);
boxSouth.add(filterText);
boxSouth.add(filterButton);
// place GUI components on JFrame's content pane
JFrame window = new JFrame("Displaying Query Results");
window.add(boxNorth, BorderLayout.NORTH);
window.add(new JScrollPane(resultTable), BorderLayout.CENTER);
window.add(boxSouth, BorderLayout.SOUTH);
// create event listener for submitButton
submitButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// perform a new query
try
{
tableModel.setQuery(queryArea.getText());
}
catch (SQLException sqlException)
{
JOptionPane.showMessageDialog(null,
sqlException.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE);
// try to recover from invalid user query
// by executing default query
try
{
tableModel.setQuery(DEFAULT_QUERY);
queryArea.setText(DEFAULT_QUERY);
}
catch (SQLException sqlException2)
{
JOptionPane.showMessageDialog(null,
sqlException2.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE);
// ensure database connection is closed
tableModel.disconnectFromDatabase();
System.exit(1); // terminate application
}
}
}
}
); // end call to addActionListener
final TableRowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(tableModel);
resultTable.setRowSorter(sorter);
// create listener for filterButton
filterButton.addActionListener(
new ActionListener()
{
// pass filter text to listener
public void actionPerformed(ActionEvent e)
{
String text = filterText.getText();
if (text.length() == 0)
sorter.setRowFilter(null);
else
{
try
{
sorter.setRowFilter(
RowFilter.regexFilter(text));
}
catch (PatternSyntaxException pse)
{
JOptionPane.showMessageDialog(null,
"Bad regex pattern", "Bad regex pattern",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
); // end call to addActionLister
// dispose of window when user quits application (this overrides
// the default of HIDE_ON_CLOSE)
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.setSize(500, 250);
window.setVisible(true);
// ensure database is closed when user quits application
window.addWindowListener(
new WindowAdapter()
{
public void windowClosed(WindowEvent event)
{
tableModel.disconnectFromDatabase();
System.exit(0);
}
}
);
}
catch (SQLException sqlException)
{
JOptionPane.showMessageDialog(null, sqlException.getMessage(),
"Database error", JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(1); // terminate application
}
}
}