-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationWindow.java
More file actions
217 lines (168 loc) · 6.58 KB
/
ConfigurationWindow.java
File metadata and controls
217 lines (168 loc) · 6.58 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
208
209
210
211
212
213
214
215
216
217
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ConfigurationWindow extends JFrame {
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Variables
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private static final long serialVersionUID = 1L;
// GUI variables
private JPanel contentPane;
private JLabel lblEnterCommand;
private JScrollPane scrollPane;
private JTextArea textArea;
private JButton btnSend;
private JSplitPane splitPane;
private JButton btnConnect;
private JButton btnDisconnect;
private JButton btnClear;
// Other
private static boolean connected;
private String command;
private DatabaseInterface db = new DatabaseInterface();
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Action Listeners
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private ActionListener btnConnectListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
connect();
}
};
private ActionListener btnDisconnectListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
disconnect();
}
};
private ActionListener btnSendListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
sendCommand();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
private ActionListener btnClearListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearTextArea();
}
};
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Launch the application.
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ConfigurationWindow frame = new ConfigurationWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create the frame.
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public ConfigurationWindow() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textArea = new JTextArea(12, 40);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(10, 36, 414, 93);
contentPane.add(scrollPane);
lblEnterCommand = new JLabel("Enter Command");
lblEnterCommand.setBounds(157, 11, 110, 14);
contentPane.add(lblEnterCommand);
btnSend = new JButton("Send");
btnSend.setBounds(10, 140, 89, 23);
contentPane.add(btnSend);
splitPane = new JSplitPane();
splitPane.setBounds(130, 226, 200, 25);
contentPane.add(splitPane);
btnConnect = new JButton("Connect");
splitPane.setLeftComponent(btnConnect);
btnDisconnect = new JButton("Disconnect");
splitPane.setRightComponent(btnDisconnect);
btnClear = new JButton("Clear");
btnClear.setBounds(335, 140, 89, 23);
contentPane.add(btnClear);
// Listeners
btnConnect.addActionListener(btnConnectListener);
btnDisconnect.addActionListener(btnDisconnectListener);
btnSend.addActionListener(btnSendListener);
btnClear.addActionListener(btnClearListener);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Utils Methods
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void connect() {
// DatabaseInterface db = new DatabaseInterface();
connected = db.connect();
// Verify connection
System.out.println(connected);
}
private void disconnect() {
// DatabaseInterface db = new DatabaseInterface();
connected = db.disconnect();
// Verify connection
System.out.println(connected);
}
private void sendCommand() throws SQLException {
command = new String(textArea.getText());
System.out.println(command);
if (!connected) {
connected = db.connect();
}
// Create a query and send the data to the database
// Example set a user offline
ResultSet res = null;
try {
res = db.runQuery(command);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Sending Failed");
}
// Try for example the command: SELECT * FROM subject WHERE Name =
// 'Yasser'
while (res.next()) {
String name = res.getString("Name");
String weight = res.getString("Weight");
String height = res.getString("Height");
Date date = res.getDate("Birthday");
String gender = res.getString("Gender");
String online = res.getString("Online");
System.out.println("Name: " + name);
System.out.println("Weight: " + weight);
System.out.println("Height: " + height);
System.out.println("Birthday: " + date);
System.out.println("Gender: " + gender);
System.out.println("Online: " + online);
}
}
private void clearTextArea() {
textArea.setText(null);
}
}