Source code: com/loihl/sqltool/QueryResultPanel.java
1 package com.loihl.sqltool;
2
3 import com.loihl.swing.MessageDialog;
4 import java.util.Properties;
5 import java.rmi.Naming;
6 import java.rmi.RemoteException;
7 //import java.awt.*;
8 import java.awt.BorderLayout;
9 import javax.swing.JFrame;
10 import javax.swing.JPanel;
11 import javax.swing.JSplitPane;
12 import javax.swing.JScrollPane;
13 import javax.swing.JTextArea;
14 import javax.swing.JTable;
15 import javax.swing.table.DefaultTableModel;
16 import javax.swing.text.PlainDocument;
17 import javax.swing.border.BevelBorder;
18 import java.sql.SQLException;
19
20 /**
21 * Class QueryResultTabPane
22 * @author Robert J. Loihl
23 * @version 0.5.0
24 */
25 public class QueryResultPanel extends JPanel{
26 //Member Variables
27 //GUI components
28 private JSplitPane splitPane;
29 private JScrollPane queryPane;
30 private JTextArea queryTextArea;
31 private JScrollPane resultPane;
32 private JTable resultTable;
33 //Query object
34 /**
35 * @clientCardinality 1
36 * @supplierCardinality *
37 * @label Uses
38 */
39 private Query query;
40 //get from system props
41 private Properties userProps = null;
42 private String userName = "";
43 private String password = "";
44 private String dBUrl = "jdbc:mysql://marge.home/consult";
45 private String driverName = "org.gjt.mm.mysql.Driver";
46 private String rmiServerUrl = "rmi://localhost/RemoteQueryFactory";
47 private boolean parametersChanged = false;
48
49 private static final String USER_LABEL = "Login";
50 private static final String PASS_LABEL = "Password";
51 /**
52 * @clientCardinality 1
53 * @label Uses
54 * @supplierCardinality 0..1
55 * @directed
56 */
57 /*#ConnectionDialog lnkConnectionDialog;*/
58 /**
59 * @clientCardinality 1
60 * @label Uses
61 * @supplierCardinality 0..1
62 * @directed
63 */
64 /*#QueryFactory lnkQueryFactory;*/
65 /**
66 * Method: AdHocQueryPanel
67 * Description: constructor for AdHocQueryPanel class
68 */
69 public QueryResultPanel(){
70 super();
71 init();
72 }
73 /**
74 * Method: init
75 * Description: Establish all of the components
76 */
77 private void init(){
78 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
79 queryTextArea = new JTextArea(new PlainDocument(),"",500,300);
80 queryTextArea.setEditable(true);
81 queryPane = new JScrollPane(queryTextArea);
82 queryPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED));
83
84 //don't want to setup resulttable yet??
85 resultTable = new JTable();
86 resultPane = new JScrollPane(resultTable);
87 resultPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED));
88
89 //set layout managers??
90 this.setLayout(new BorderLayout());
91 //now put the components together
92
93 this.splitPane.setTopComponent(this.queryPane);
94 this.splitPane.setBottomComponent(this.resultPane);
95 this.splitPane.setDividerLocation(100);
96 this.add(this.splitPane, BorderLayout.CENTER);
97
98 userProps = new Properties();
99 userProps.put(USER_LABEL, userName);
100 userProps.put(PASS_LABEL, password);
101
102 }
103
104 /**
105 * Method: clearQueryText
106 * Description: clear the query text area
107 */
108 protected void clearQueryText(){
109 //clear the sql input JTextArea
110 queryTextArea.setText("");
111 //clear the Result Table
112 }
113
114 /**
115 * Method: executeQueryText
116 * Description: execute the current query text
117 */
118 protected void executeQueryText(){
119 if ((rmiServerUrl == null)||
120 (rmiServerUrl.trim().equals(""))||
121 (dBUrl == null)||
122 (dBUrl.trim().equals(""))||
123 (driverName == null) ||
124 (driverName.trim().equals(""))||
125 (userProps.getProperty(USER_LABEL).trim().equals("")) ||
126 (userProps.getProperty(PASS_LABEL).trim().equals(""))){
127 setQueryConnection();
128 }
129 DefaultTableModel dtm = null;
130 //get the text from the JTextArea parse it,
131 //execute it and then display the resultset
132 String sqlStmt = queryTextArea.getText();
133 //parse & validate the statement syntax here
134
135 //execute the query and get the ResultSet
136 try{
137 dtm = query.execute(sqlStmt);
138 }catch (RemoteException e){
139 String errorString = "RemoteException while " +
140 "executing Query Text. Error in " + e.toString();
141 //MessageDialog md = new MessageDialog((JFrame)(
142 //instantiate MessageDialog
143 new MessageDialog((JFrame)(
144 (JFrame)(this.getParent().getParent().getParent().getParent().getParent().getParent())),
145 "Remote Exception",
146 true,
147 errorString);
148 } catch (SQLException se){
149 String errorString = new String("SQL Exception Raised " +
150 "executing Query Text. Error in " + se.toString());
151 //MessageDialog md = new MessageDialog((JFrame)(
152 //instantiate MessageDialog
153 new MessageDialog((JFrame)(
154 this.getParent().getParent().getParent().getParent().getParent().getParent()),
155 "SQL Exception",
156 true,
157 errorString);
158 }
159 // build the JTable
160 resultTable = new JTable(dtm);
161
162 //add it to the Viewport for the JScrollpane
163 resultPane.setViewportView(resultTable);
164
165 }
166
167 /**
168 * Method: setQueryConnection
169 * Description: get the connection parameters
170 * and establish the connection to the database
171 */
172 protected void setQueryConnection(){
173 //System.out.println("Getting ConnectionParameters");
174 //ConnectionDialog cd = new ConnectionDialog(this,
175 new ConnectionDialog(this, "Connection Settings", true,
176 userProps.getProperty(QueryResultPanel.USER_LABEL),
177 userProps.getProperty(QueryResultPanel.PASS_LABEL),
178 rmiServerUrl,
179 dBUrl,
180 driverName);
181 System.out.println("Done Getting ConnectionParameters");
182 if (parametersChanged){
183 parametersChanged = false;
184 getRemoteQuery();
185 }
186 }
187
188
189 protected void getRemoteQuery(){
190
191 try{
192 //Getting Remote Query Factory from rmiServer
193 QueryFactory qf = (QueryFactory)Naming.lookup(rmiServerUrl);
194 //Getting Remote Query from QueryFactory
195 query = (Query)qf.getQuery();
196 } catch (Exception e) {
197 //MessageDialog md = new MessageDialog((JFrame)(
198 //instantiate MessageDialog
199 new MessageDialog((JFrame)(
200 (JFrame)(this.getParent().getParent().getParent().getParent().getParent().getParent())),
201 "Error",
202 true,
203 "Error in " + e.toString());
204 parametersChanged = true;
205 }
206 try{
207 System.out.println("Setting Connection");
208 query.setConnection(dBUrl, driverName, userProps);
209 parametersChanged = true;
210 } catch (RemoteException e) {
211 String errorString = new String("RemoteException while setting " +
212 "the database connection. The connection to the remote " +
213 "object server may have been lost. Error in " +
214 e.toString());
215 //MessageDialog md = new MessageDialog((JFrame)(
216 //instantiate MessageDialog
217 new MessageDialog((JFrame)(
218 this.getParent().getParent().getParent().getParent().getParent().getParent()),
219 "Connection Exception",
220 true,
221 errorString);
222 parametersChanged = true;
223 } catch (SQLException se){
224 String errorString = new String("SQL Exception Raised " +
225 "establishing Connection to database.\n Error in :\n" +
226 se.toString());
227 //MessageDialog md = new MessageDialog((JFrame)(
228 //instantiate MessageDialog
229 new MessageDialog((JFrame)(
230 this.getParent().getParent().getParent().getParent().getParent().getParent()),
231 "Connection Exception",
232 true,
233 errorString);
234 //System.out.println(errorString);
235 parametersChanged = true;
236 }
237 }
238
239
240 protected void setConnectionParameters(String user, String password,
241 String dbURL, String dbdriver, String rmiSvrURL)
242 throws Exception{
243 if ((user == null)&&(user.trim().equals(""))){
244 throw new Exception("Invalid User Name Format");
245 }
246 else if ((password == null)&&(password.trim().equals(""))){
247 throw new Exception("Invalid Password Format");
248 }
249 else if ((dbURL == null)||(dbURL.trim().equals(""))){
250 throw new Exception("Invalid Database URL Format");
251 }
252 else if ((dbdriver == null)||(dbdriver.trim().equals(""))){
253 throw new Exception("Invalid Database Driver Format");
254 }
255 else if ((rmiSvrURL == null)||(rmiSvrURL.trim().equals(""))){
256 throw new Exception("Invalid rmiRegistry Server URL Format");
257 }
258 else if ((!user.equals(userProps.getProperty(USER_LABEL))) ||
259 (!password.equals(userProps.getProperty(PASS_LABEL))) ||
260 (!dbURL.equals(dBUrl)) ||
261 (!dbdriver.equals(driverName)) ||
262 (!rmiSvrURL.equals(rmiServerUrl)) ){
263 userProps.setProperty(USER_LABEL,user);
264 userProps.setProperty(PASS_LABEL,password);
265 System.out.println("Username=<" + userProps.getProperty(USER_LABEL) +
266 "> Password=<"+userProps.getProperty(PASS_LABEL)+">");
267 dBUrl = dbURL;
268 driverName = dbdriver;
269 rmiServerUrl = rmiSvrURL;
270 parametersChanged = true;
271 }
272 }
273 }