/Users/lyon/j4p/src/rdbms/MetaBeanFrame.java
|
1 package rdbms;
2
3 import java.awt.event.WindowEvent;
4
5 /**
6 * Facade pattern implementation
7 * for JDBC
8 * This init method creates the
9 * connection to the database and retrieves
10 * additional database information.
11 * @version 1.00
12 */
13
14 public class MetaBeanFrame extends javax.swing.JFrame {
15 private String url = null;
16 private String driver = null;
17
18 private String userId = null;
19 private String password = null;
20 private java.sql.Connection c = null;
21 private java.sql.Statement statement = null;
22 private java.sql.DatabaseMetaData dbmd = null;
23 private java.sql.ResultSet rs = null;
24 private java.sql.ResultSetMetaData rsmd = null;
25 private java.sql.ResultSet rs_tabletypes = null;
26
27 private boolean isReadOnly = false;
28 private boolean usesLocalFiles = false;
29 private String driverName = null;
30 private String catalogName = null;
31 private String productName = null;
32 private String keyWords = null;
33 private String tablename = null;
34 private String currentquery = null;
35
36 private String tableFields[] = null;
37 private String tableNames[] = null;
38 private String nonMSysTables[] = null;
39
40 /**
41 * Determines if database
42 * uses a local file.
43 *
44 * The value is set using the DatabaseMetaData.
45 * usesLocalFiles() method
46 * contained in the java.sql package.
47 * This method is called and this value is
48 * set in this classes init() method.
49 *
50 * @return true if it does, false if not
51 */
52 public boolean getUsesLocalFiles() {
53 return usesLocalFiles;
54 }
55
56 /**
57 * Gets whether or not this connection
58 * to this database is read only.
59 *
60 * The value is set using the
61 * Connection.isReadOnly() method
62 * contained in the java.sql package.
63 * This method is called and this value is
64 * set in this classes init() method.
65 *
66 * @return true if it is, false if not
67 */
68 public boolean getReadOnly() {
69 return isReadOnly;
70 }
71
72 /**
73 * Gets this Connection's current catalog name.
74 *
75 * The value is set using the
76 * Connection.getCatalog() method
77 * contained in the java.sql package. This method is called and this value is
78 * set in this classes init() method.
79 *
80 * @return the current catalog name or null */
81 public String getCatalogName() {
82 return catalogName;
83 }
84
85 /**
86 * Gets the name of this JDBC driver.
87 *
88 * The value is set using the
89 * DatabaseMetaData.getDriverName() method
90 * contained in the java.sql package.
91 * This method is called and this value is
92 * set in this classes init() method.
93 *
94 * @return the JDBC Driver name
95 */
96 public String getDriverName() {
97 return driverName;
98 }
99
100 /**
101 * Gets the name of this database product.
102 *
103 * The value is set using the
104 * DatabaseMetaData.getDatabaseProductName() method
105 * contained in the java.sql package.
106 * This method is called and this value is
107 * set in this classes init() method.
108 *
109 * @return the database product name
110 */
111 public String getProductName() {
112 return productName;
113 }
114
115 /**
116 * Gets the ResultSet contained
117 * in this instance variable rs.
118 *
119 * @return this classes ResultSet
120 */
121 public java.sql.ResultSet getResultSet() {
122 return rs;
123 }
124
125
126 /**
127 * Gets the value of this current table name.
128 *
129 * The value is set using the
130 * SqlBean.setTableName() method
131 *
132 * @return the current table name
133 */
134 public String getTableName() {
135 return tablename;
136 }
137
138 /**
139 * Gets the value of this current SQL.
140 *
141 * The value is set using the
142 * SqlBean.query() method
143 *
144 * @return the current SQL query
145 */
146 public String getCurrentQuery() {
147 return currentquery;
148 }
149
150
151 /**
152 * Gets the table names contained in
153 * this current database.
154 *
155 * The table names are placed in a
156 * ResultSet using the DatabaseMetaData.
157 * getTables() method
158 * From the ResultSet, the tables are
159 * added to a vector and then
160 * converted into a String array.
161 * This method can be used
162 * at anytime after the init()
163 * method is called to set the DataBaseMetaData.
164 *
165 * @return the table names
166 */
167 public String[] getTableNames(java.sql.DatabaseMetaData dbmd, String userid) {
168 java.util.Vector tableVector = new java.util.Vector();
169 try {
170 rs = dbmd.getTables(null, userid, null, null);
171 rsmd = rs.getMetaData();
172 while (rs.next())
173
174 tableVector.addElement(rs.getString("TABLE_NAME"));
175 } catch (java.sql.SQLException e) {
176 print(e);
177 }
178 int n = tableVector.size();
179 tableNames = new String[n];
180 for (int i = 0; i < n; i++)
181 tableNames[i] = (String) tableVector.elementAt(i);
182 return tableNames;
183 }
184
185
186 /**
187 * Gets the names of all the Non-System Tables in this Database.
188 *
189 * Retrieves all the tables using the
190 * getTableNames() method.
191 * Then uses the
192 * getNumberOfMSysTables() to determine the number of
193 * SystemTables contained in the
194 * database and places those names in a String array.
195 * This method can be used at anytime
196 * after the init() method is called to
197 * set the DataBaseMetaData.
198 *
199 * @return the names of the non-system tables
200 */
201 /* public String[] getNonMSysTables() {
202
203 String tn[] = getTableNames();
204 int n = tableNames.length - getNumberOfMSysTables();
205 nonMSysTables = new String[n];
206 for (int i=0; i < n; i++)
207 nonMSysTables[i]= tn[i+getNumberOfMSysTables()];
208
209 return nonMSysTables;
210 }
211 */
212
213 /**
214 * Gets the number of the System Tables in this Database.
215 *
216 * Used to help determine the table names
217 * in the getNonMSysTables() method.
218 * Determine the number of SystemTables
219 * contained in the database my searching for
220 * names starting with MSys.
221 *
222 * @return the number of system tables
223 */
224 private int getNumberOfMSysTables() {
225
226 int k = 0;
227 for (int i = 0; i < tableNames.length; i++)
228 if (tableNames[i].startsWith("MSys"))
229 k++;
230 return k;
231 }
232
233 /**
234 * Gets the table types available in
235 * this database connection. The results
236 * are ordered by table type. *
237 * The results are ordered by table type.
238 * Typical types are:
239 * <br>"TABLE", "VIEW", "SYSTEM TABLE",
240 * "GLOBAL", "TEMPORARY",
241 "LOCAL TEMPORARY", "ALIAS", "SYNONYM". *
242 * @return the current SQL query
243 */
244
245 public java.sql.ResultSet getTableTypes() {
246 return rs_tabletypes;
247 }
248
249
250
251 /**
252 * Sets this classes resultset instance variable,
253 * rs, based on a provided SQL query.
254 *
255 * @param myquery the SQL query
256 */
257 // public void setResultSet(String myquery) {
258 // rs = query(myquery);
259 // }
260
261
262 /**
263 * Sets this classes instance variable,
264 * userId, for this database connection.
265 *
266 * This is the database user on whose behalf the Connection is being made for
267 *
268 * @param _userId the database UserId
269 */
270 public void setUserId(String _userId) {
271 userId = _userId;
272 }
273
274 /**
275 * Sets this classes instance variable,
276 * password, for this database connection.
277 *
278 * The password associated with the database
279 * user on whose behalf the
280 * Connection is being made for.
281 *
282 * @param _password the database Password */
283 public void setPassword(String _password) {
284 password = _password;
285 }
286
287 /**
288 * Sets this classes instance variable, url, for this database url.
289 * The url is in the form of jdbc:subprotocol:subname
290 * @param _url the database url
291 */
292 public void setUrl(String _url) {
293 url = _url;
294 }
295
296 /**
297 * Sets the name of the instance variable, driver, which this class is loadeding.
298 *
299 * This is the string representation of the driver being loaded to
300 make the connection to the database.
301 *
302 * @param _driver the driver name
303 */
304 public void setDriver(String _driver) {
305 driver = _driver;
306 }
307
308 /**
309 * Sets this classes instance variable, tablename,
310 * for the current database tablename. *
311 * @param _tablename the database tablename
312 */
313 public void setTableName(String _tablename) {
314 tablename = _tablename;
315 }
316
317 /**
318 * Constructor.
319 */
320 public MetaBeanFrame() {
321 }
322
323 /**
324 * Loads specified driver and initializes the
325 * Connection to this Database.
326 *
327 * <p>Loads the driver and Connects to the Database
328 * <br>Retrieves and sets Database/
329 Connection Information as follows:
330 * <br>   MetaData of this Database
331 * <br>   Read Only Property
332 * <br>   Uses Local Files Property
333 * <br>   Driver Name Used for the Connection
334 * <br>   Database Product Name
335 * <br>   Table Types in this Database */
336
337 public void init() {
338 try {
339
340 // ************ uncomment the following in order to work @ home
341 // DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
342 // c = DriverManager.getConnection(url,userId,password);
343 Class.forName(driver);
344 c = java.sql.DriverManager.getConnection(url, userId, password);
345 dbmd = c.getMetaData();
346 catalogName = c.getCatalog();
347 isReadOnly = c.isReadOnly();
348 usesLocalFiles = dbmd.usesLocalFiles();
349 driverName = dbmd.getDriverName();
350 productName = dbmd.getDatabaseProductName();
351 rs_tabletypes = dbmd.getTableTypes();
352
353 } catch (ClassNotFoundException e) {
354 println(e);
355 } catch (java.sql.SQLException e) {
356 print(e);
357 }
358 System.out.println("Opened Connection:" + url);
359 }
360
361 /**
362 * Prints information about this database connection.
363 * <p>
364 * <br>Prints the following information:
365 * <br>   The name of this database product
366 * <br>   This Connection's current catalog name
367 * <br>   Is this connection in read-only mode
368 * <br>   Does this database store tables in a local files
369 * <br>   The name of this JDBC driver
370 * <br>   The SQL keywords of this database
371 * <br>   The table types of all the tables in this database
372 * <br>   The names of all the tables in this database
373 * <br>   The names of all the non-system tables in this database
374 */
375 public void printDataBaseInfo() {
376 println("*****");
377 println("productName=" + productName);
378 println("*****");
379 println("catalogName=" + catalogName);
380 println("*****");
381 println("is ReadOnly=" + getReadOnly());
382 println("*****");
383 println("usesLocalFiles=" + getUsesLocalFiles());
384 println("*****");
385 println("driverName=" + driverName);
386 println("*****");
387 println("Non SL92 keywords:");
388 println(getKeyWords());
389 println("*****");
390 println("TableTypes:");
391 print(getTableTypes());
392 println("*****");
393 println("TableNames - All:");
394 // println(getTableNames());
395 println("*****");
396 println("TableNames - NonSystem:");
397 // println(getNonMSysTables());
398
399
400 }
401
402 /**
403 * Prints information about this current ResultSet.
404 * <p>
405 * <br>Prints the following information:
406 * <br>   The column names (fields) of this database
407 * <br>   The type name of the columns (fields) used by this database
408 *
409 * @param _rs the current ResultSet
410 */
411 public void printResultSetInfo(java.sql.ResultSet _rs) {
412 println("*****");
413 println("Column Names:");
414 println(getTableFields(getResultSetMetaData(_rs)));
415 println("*****");
416 println("Column Types:");
417 println(getColumnTypeNames(getResultSetMetaData(_rs)));
418 println("*****");
419 println("Number of Rows:");
420 //println(Integer.toString((getNumberofRows(this.get(_rs)))));
421 println("*****");
422 println("Print The First Row:");
423 }
424
425 /**
426 * Closes the connections to this database. */
427 public void close() {
428
429 try {
430
431 rs.close();
432 rs_tabletypes.close();
433 statement.close();
434 c.close();
435
436 System.out.println("closed connection");
437 } catch (java.sql.SQLException e) {
438 print(e);
439 }
440 }
441
442 /**
443 * Creates a Result Set based on an sql query. *
444 * @param c the sql query
445 * @return the ResultSet
446 */
447 public java.sql.ResultSet query(java.sql.Connection c, String sql) {
448
449 try {
450 statement = c.createStatement();
451 currentquery = sql;
452 return statement.executeQuery(sql);
453 } catch (java.sql.SQLException e) {
454 print(e);
455 }
456 return null;
457 }
458
459 /**
460 * Gets the MetaData for a specified ResultSet. *
461 * @param _rs the ResultSet
462 * @return the ResultSetMetaData
463 */
464 public java.sql.ResultSetMetaData getResultSetMetaData(java.sql.ResultSet _rs) {
465 try {
466 return _rs.getMetaData();
467 } catch (java.sql.SQLException e) {
468 print(e);
469 }
470 return null;
471 }
472
473 /**
474 * Gets the number of columns in a ResultSet. *
475 * @param _rsmd the ResultSetMetaData
476 * @return number of Columns (fields)
477 */
478 public int getColumnCount(java.sql.ResultSetMetaData _rsmd) {
479 try {
480 return _rsmd.getColumnCount();
481 } catch (java.sql.SQLException e) {
482 print(e);
483 }
484 return 0;
485 }
486
487 /**
488 * Gets the keywords associated with this database. *
489 * @return the keywords
490 */
491
492 public String getKeyWords() {
493
494 try {
495 return dbmd.getSQLKeywords();
496 } catch (java.sql.SQLException e) {
497 print(e);
498 }
499 return null;
500 }
501
502 /**
503 * Gets the database types of the columns in a ResultSet.
504 *
505 * These are the type name used by this database. If the column type is a user-defined type,
506 * then a fully-qualified type name is returned. *
507 * @param _rsmd the ResultSetMetaData
508 * @return the column types
509 */
510
511 public String[] getColumnTypeNames(java.sql.ResultSetMetaData _rsmd) {
512
513 int count = getColumnCount(_rsmd);
514
515 String sa [] = new String[count];
516 try {
517 for (int i = 0; i < sa.length; i++) {
518 sa[i] = _rsmd.getColumnTypeName(i + 1);
519 }
520 } catch (java.sql.SQLException e) {
521 print(e);
522 }
523 return sa;
524 }
525
526 /**
527 * Converts a row in a ResultSet to a String.
528 *
529 * @param _rs the ResultSet pointed to a particular row
530 * @return the contents of the ResultSet
531 */
532 public String[] getRowAsString(java.sql.ResultSet _rs) {
533
534 int N = getColumnCount(getResultSetMetaData(_rs));
535 String s[] = new String[N];
536 try {
537 for (int i = 0; i < N; i++)
538 s[i] = _rs.getString(i + 1);
539 } catch (java.sql.SQLException e) {
540 print(e);
541 }
542 return s;
543 }
544
545 /**
546 * Converts a ResultSet to a Vector of Strings.
547 * @param _rs the ResultSet
548 * @return the vector containing the
549 * ResultSet with each row as a String
550 * /
551
552 public java.util.Vector getRows(java.sql.ResultSet _rs) {
553 java.util.Vector v = new java.util.Vector();
554 while (nextRow(_rs))
555 v.addElement(getRowAsString(_rs));
556 return v;
557 }
558
559 /**
560 * Returns the size of a vector.
561 Used with the getRows() method that converts a ResultSet to a vector.
562 *
563 * @param v the Vector
564 * @return the number of rows
565 */
566 public int getNumberofRows(java.util.Vector v) {
567 return v.size();
568 }
569
570 /**
571 * Moves to the top of this RecordSet.
572 *
573 * @param _rs the ResultSet
574 */
575 public void moveToTop(java.sql.ResultSet _rs) {
576 try {
577 //_rs.top();
578 // not implemented in 1.2
579 throw new java.sql.SQLException("Not implemented until jdk1.2");
580 } catch (java.sql.SQLException e) {
581 print(e);
582 }
583 }
584
585 /**
586 * Prints the contents of a provided ResultSet. *
587 * @param _rs the ResultSet
588 */
589 public void print(java.sql.ResultSet _rs) {
590 int i;
591 String cn[] = getTableFields(getResultSetMetaData(_rs));
592 println(cn);
593 boolean more = false;
594 while (more = nextRow(_rs))
595 println(getRowAsString(_rs));
596
597 }
598
599 public java.util.Vector getRecords(java.sql.ResultSet _rs) {
600 java.util.Vector v = new java.util.Vector();
601 v.addElement(getTableFields(getResultSetMetaData(_rs)));
602 // println(cn);
603
604 while (nextRow(_rs))
605 // println(getRowAsString(_rs));
606 v.addElement(getRowAsString(_rs));
607 return v;
608 }
609
610 /**
611 * Prints the contents of a provided Object. * Uses the toString() method for the
612 * provided object.
613 *
614 * @param o the Object
615 */
616
617 public void println(Object o) {
618
619 System.out.println(o);
620 }
621
622 /**
623 * Prints the contents of a String array.
624 *
625 * @param s the array
626 */
627
628 public void println(String s[]) {
629
630 for (int i = 0; i < s.length; i++)
631 System.out.print(s[i] + '\t');
632 System.out.println();
633
634 }
635
636 /**
637 * Prints messages about this SQL Exception. *
638 * @param ex the exception
639 */
640
641 private void print(java.sql.SQLException ex) {
642 println("\n*** SQLException caught ***\n");
643 while (ex != null) {
644 println("SQLState: " + ex.getSQLState());
645 println("Message: " + ex.getMessage());
646 println("Vendor: " + ex.getErrorCode());
647 ex = ex.getNextException();
648 println("");
649 }
650 ex.printStackTrace();
651 }
652
653
654 /**
655 * Moves to the next row of a provided ResultSet. *
656 * @param _rs the ResultSet
657 */
658 public boolean nextRow(java.sql.ResultSet _rs) {
659 try {
660 return _rs.next();
661 } catch (java.sql.SQLException e) {
662 return false;
663 }
664 }
665
666 /**
667 * Gets the names of the columns (fields)
668 * in a provided ResultSet. *
669 * @param _rsmd the ResultSetMetaData
670 * @return the table names
671 */
672 public String[] getTableFields(java.sql.ResultSetMetaData _rsmd) {
673
674 String s [] = new String[getColumnCount(_rsmd)];
675 try {
676 for (int i = 1; i <= s.length; i++)
677 s[i - 1] = _rsmd.getColumnLabel(i);
678 } catch (java.sql.SQLException e) {
679 print(e);
680 }
681 tableFields = s;
682 return s;
683 }
684
685 /**
686 * Gets this DataBaseMetaData object.
687 *
688 * @return this DataBaseMetaData
689 */
690
691 public java.sql.DatabaseMetaData getDatabaseMetaData() {
692
693 return dbmd;
694 }
695
696 /**
697 * Process an SQL INSERT, DELETE or UPDATE statement string.
698 *
699 * Additionally, this could be an SQL
700 * statement that returns nothing such as SQL DDL statments.
701 *
702 * @param sql the SQL INSERT, UPDATE or DELETE statement
703 * or an SQL statement that returns nothing
704 */
705
706 public void modifyDatabase(java.sql.Connection c, String sql) {
707
708 try {
709 java.sql.Statement s = c.createStatement();
710 int updateResult = s.executeUpdate(sql);
711 javax.swing.JOptionPane.showMessageDialog(
712 null,
713 "Update is Successful \n" + c,
714 "Message",
715 javax.swing.JOptionPane.INFORMATION_MESSAGE);
716
717 println("UPDATE SUCCESSFUL");
718 println("SQL Statment= " + sql);
719 } catch (java.sql.SQLException e) {
720 javax.swing.JOptionPane.showMessageDialog(
721 null,
722 "Update not Successful \n" + e,
723 "Error Message",
724 javax.swing.JOptionPane.INFORMATION_MESSAGE);
725
726 }
727 }
728
729 }
730
731 /**
732 *abstract class Runbutton extends Button class and implements
733 *Actionlistener and Runnable interfaces. This class is used create runnable buttons.
734 * gui.run method is called in the action event method.
735 */
736
737 abstract class RunButton
738 extends java.awt.Button
739 implements java.awt.event.ActionListener,
740 Runnable {
741 RunButton(String s) {
742 super(s);
743 addActionListener(this);
744 }
745
746 public void actionPerformed(
747 java.awt.event.ActionEvent e) {
748 run();
749 }
750 }
751
752 /**
753 *The class RdbmsUtil extends MetaBean and implements Action listener interface
754 *
755 */
756
757 class RdbmsUtil extends MetaBeanFrame implements java.awt.event.ActionListener {
758 // Frame properties
759 private java.awt.GridBagConstraints gbCons;
760 private java.awt.GridBagLayout gbLayout;
761 private java.awt.Container cont;
762 private javax.swing.JScrollPane tablePanel;
763 private javax.swing.JPanel dsnPanel,browsePanel,buttonPanel,innerDsnPanel,tabListPanel,newRecordsPanel;
764 private javax.swing.JLabel username;
765 private javax.swing.JLabel password;
766 private javax.swing.JList databaseList, tableList;
767 private javax.swing.JTextField tfUserid, tfPassword;
768 public javax.swing.JTextField newRow[], tf[];
769 public boolean connected = false, inserted = false, updated = false;
770
771 /** connectivity properites
772 * the following url is using oracle jdbc driver to access oracle databases
773 * oracle jdbc driver is downloaded from www.oracle.com and installed
774 * in order to access oracle 7.x and oracle8i databases
775 */
776 private String url = "jdbc:oracle:oci8:@"; // work on oracle jdbc driver
777 //private String driver = "jdbc.oracle.oci8:@"; // work on oracle jdbc driver
778 //private String userId = "scott";
779 //private String passWord = "tiger";
780 public java.sql.Connection conn;
781 private java.sql.DatabaseMetaData dbmd, tableDbmd;
782 private java.sql.ResultSetMetaData rsmd,tableRsmd;
783 private boolean isReadOnly = false;
784 private boolean usesLocalFiles = false;
785 private String driverName = null;
786 private String catalogName = null;
787 private String productName = null;
788
789 public String columnNames [] = null, columnTypes [] = null;
790 private java.util.Vector recordsVector,updateStmtVector;
791 javax.swing.JPanel innerPanel;
792 public int rows, cols, newRows = 0;
793 public String tableName;
794 public int currentRow, currentCol;
795 // default tables and databases
796 String tables[] = {"Employee ", "Department ", "Salary ", "Bonus ", "abc", "def", "GHI", "XYZ"};
797 String databases[] = {"Production", "Development", "pulsed", "stst", "stqa", "stdv", "hpstqa", "Foxpro", "MySql", "Access"};
798
799 // constructor
800 RdbmsUtil() {
801 // set the layout
802 cont = getContentPane();
803 cont.setLayout(new java.awt.BorderLayout());
804
805 // new panel
806 //javax.swing.JPanel midPanel = new javax.swing.JPanel();
807
808 // define dsnpanel
809 dsnPanel = new javax.swing.JPanel();
810 innerDsnPanel = new javax.swing.JPanel();
811 tabListPanel = new javax.swing.JPanel();
812 dsnPanel.setLayout(new java.awt.FlowLayout());
813 innerDsnPanel.setLayout(new java.awt.GridLayout(0, 1));
814
815 // defind tablepanel
816 tablePanel = new javax.swing.JScrollPane();
817
818 // define buttonpanel
819 buttonPanel = new javax.swing.JPanel();
820 buttonPanel.setLayout(new java.awt.GridLayout(1, 0));
821
822 // define browse panel
823 gbLayout = new java.awt.GridBagLayout();
824 browsePanel = new javax.swing.JPanel();
825 innerPanel = new javax.swing.JPanel();
826 newRecordsPanel = new javax.swing.JPanel();
827 browsePanel.add(innerPanel);
828
829 gbCons = new java.awt.GridBagConstraints();
830
831 // components
832
833 tableList = new javax.swing.JList(tables);
834 databaseList = new javax.swing.JList(databases);
835 tableList.setVisibleRowCount(5);
836 databaseList.setVisibleRowCount(5);
837 databaseList.setFixedCellWidth(200);
838 tableList.setFixedCellWidth(300);
839
840 tableList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
841 databaseList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
842 // properties to connect database and its values are defined
843 tfUserid = new javax.swing.JTextField("scott");
844 tfPassword = new javax.swing.JPasswordField("tiger");
845
846 tfUserid.setEditable(true);
847 tfPassword.setEditable(true);
848
849 javax.swing.JLabel user = new javax.swing.JLabel("User Name :");
850 javax.swing.JLabel pass = new javax.swing.JLabel("Password :");
851
852 // runnable buttons are created
853 // runnable button "query" is created
854 java.awt.Button connect = new RunButton("Connect") {
855 public void run() {
856 exConnect();
857 connected = true;
858
859 }
860 };
861
862 // properties are added into dsnPanel
863 dsnPanel.add(new javax.swing.JScrollPane(databaseList));
864 innerDsnPanel.add(user);
865 innerDsnPanel.add(tfUserid);
866 innerDsnPanel.add(pass);
867 innerDsnPanel.add(tfPassword);
868 innerDsnPanel.add(connect);
869 dsnPanel.add(innerDsnPanel);
870 tabListPanel.add(new javax.swing.JScrollPane(tableList));
871 dsnPanel.add(tabListPanel);
872
873
874 // add runnable query button to buttonpanel
875 buttonPanel.add(new RunButton("Query") {
876 public void run() {
877 if (connected) {
878 exQuery();
879 } else {
880 javax.swing.JOptionPane.showMessageDialog(
881 null,
882 "Database is not connected!!!",
883 "Error Message",
884 javax.swing.JOptionPane.INFORMATION_MESSAGE);
885 }
886
887 }
888 });
889
890 // add runnable update button to buttonpanel
891 buttonPanel.add(new RunButton("Update") {
892 public void run() {
893 if (connected) {
894 exUpdate();
895 } else {
896 javax.swing.JOptionPane.showMessageDialog(
897 null,
898 "Database is not connected!!!",
899 "Error Message",
900 javax.swing.JOptionPane.INFORMATION_MESSAGE);
901 }
902
903 }
904 });
905
906 // add runnable insert button to buttonpanel
907 buttonPanel.add(new RunButton("Insert") {
908 public void run() {
909 if (connected) {
910 exInsert(cols);
911 } else {
912 javax.swing.JOptionPane.showMessageDialog(
913 null,
914 "Database is not connected!!!",
915 "Error Message",
916 javax.swing.JOptionPane.INFORMATION_MESSAGE);
917 }
918
919 }
920 });
921
922 // add runnable delete button to buttonpanel
923 buttonPanel.add(new RunButton("Delete") {
924 public void run() {
925 if (connected) {
926 exDelete();
927 } else {
928 javax.swing.JOptionPane.showMessageDialog(
929 null,
930 "Database is not connected!!!",
931 "Error Message",
932 javax.swing.JOptionPane.INFORMATION_MESSAGE);
933 }
934
935 }
936 });
937
938
939
940 // add runnable save button to buttonpanel
941 buttonPanel.add(new RunButton("Save") {
942 public void run() {
943 if (connected) {
944 exSave();
945 } else {
946 javax.swing.JOptionPane.showMessageDialog(
947 null,
948 "Database is not connected!!!",
949 "Error Message",
950 javax.swing.JOptionPane.INFORMATION_MESSAGE);
951 }
952
953 }
954 });
955
956
957 // add runnable exit button to buttonpanel
958 buttonPanel.add(new RunButton("Exit") {
959 public void run() {
960 if (connected) {
961 shutDown();
962 }
963 System.exit(0);
964 }
965 });
966
967 // add panels to main Panel
968 cont.add(dsnPanel, java.awt.BorderLayout.NORTH);
969 browsePanel.setSize(800, 550);
970 cont.add(new javax.swing.JScrollPane(browsePanel), java.awt.BorderLayout.CENTER);
971 cont.add(buttonPanel, java.awt.BorderLayout.SOUTH);
972 setSize(800, 500);
973 show();
974
975 }
976
977 /**
978 *Sets the current row
979 *
980 *sets the current row and col of the gui.mouse.m2.mouse pointer is focussed.
981 * these current row and column is used in the class to locate
982 * the row and process the entire row when update or delete takes place.
983 *
984 *@param row - current row
985 *@param col - current column
986 */
987
988 public void setCurrentRowCol(int row, int col) {
989 currentRow = row;
990 currentCol = col;
991 }
992
993 /**
994 *sets the color of the column
995 *
996 *sets the current column's color in blue, this change of attribute of the JTextfield is just to
997 * indicate the column is being edited.
998 *
999 *@param row - current row
1000 *@param col - current column
1001 *
1002 */
1003 public void setColumnColor(int row, int col) {
1004 // tf[row * col].setColor
1005 // tf[row * col].setcolor();
1006 }
1007
1008 /**
1009 *closed down the connection if it is open
1010 *
1011 *This method is called from exit button and window closing listener service.
1012 */
1013
1014 public void shutDown() {
1015 try {
1016 if (!conn.isClosed())
1017 conn.commit();
1018 conn.close();
1019 } catch (java.sql.SQLException e) {
1020 System.err.println("Unable to disconnect ");
1021 e.printStackTrace();
1022 }
1023 }
1024
1025 /**
1026 *connects the database per user selection
1027 *
1028 *connects the database as per the user selection
1029 *login name and password and database name are used to connect the database.
1030 * method is called to connect appropriate database
1031 */
1032 public void exConnect() {
1033 try {
1034 String db = (String) databaseList.getSelectedValue();
1035 String name = getUserId();
1036 String pass = getPassword();
1037 if (name == null || pass == null) {
1038 javax.swing.JOptionPane.showMessageDialog(
1039 null,
1040 "Invalid user name/password",
1041 "Error ",
1042 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1043
1044 throw new Exception();
1045 }
1046
1047 if (db.equals("Production")) {
1048 System.out.println(name + pass);
1049 conn = connectProduction(name, pass);
1050 } else if (db.equals("Development")) {
1051 conn = connectDevelopment(name, pass);
1052 } else {
1053 conn = connectOdbc(name, pass, db);
1054 }
1055 // get dataBaseMetaData from the connection
1056 dbmd = conn.getMetaData();
1057 catalogName = conn.getCatalog();
1058 isReadOnly = conn.isReadOnly();
1059 driverName = dbmd.getDriverName();
1060 productName = dbmd.getDatabaseProductName();
1061 // get the table names available in the database
1062 tables = getTableNames(dbmd, name.toUpperCase());
1063
1064 // populates the table names into tableList
1065 tableList.setListData(tables);
1066
1067 initArrays();
1068 show();
1069 } catch (java.sql.SQLException e) {
1070 e.printStackTrace();
1071 } catch (Exception e) {
1072 javax.swing.JOptionPane.showMessageDialog(
1073 null,
1074 "Invalid user name/password" + e,
1075 "Error Message",
1076 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1077 }
1078 System.out.println("Opened Connection :" + url);
1079 }
1080
1081 /**
1082 *gets Userid entered by the user
1083 *
1084 *@return userid - string - returns the userid
1085 */
1086
1087 public String getUserId() {
1088 return tfUserid.getText();
1089 }
1090
1091 /**
1092 *gets password entered by the user
1093 *
1094 *@return password - string - returns the password
1095 */
1096
1097 public String getPassword() {
1098 return tfPassword.getText();
1099 }
1100
1101 /**
1102 *gets the connection to production database
1103 *
1104 * production database is a oracle databse;
1105 * java uses oracle jdbc driver to
1106 * connect database. Per oracle, java has to register
1107 * oracle jdbc driver using method
1108 * DriverManager.registerDriver, and get the connection
1109 * using url "jdbc:oracle:oci8:"
1110 * database service name with @. The service name in this
1111 * script is null (@ ), because
1112 * this script access oracle8i personal edition which does
1113 * not have service name.
1114 *
1115 *@param userId - oracle database userid entered by user
1116 *@param passWord - oracle database password
1117 *
1118 *@return connection - returns connection
1119 */
1120
1121 public java.sql.Connection connectProduction(
1122 String userId,
1123 String passWord) {
1124 conn = null;
1125 try {
1126 Class c = Class.forName("oracle.jdbc.driver.OracleDriver");
1127 java.sql.DriverManager.registerDriver(
1128 (java.sql.Driver) c.newInstance());
1129 // work on oracle jdbc
1130 String url = "jdbc:oracle:oci8:@pulseD";
1131 // work on oracle jdbc driver
1132 conn = java.sql.DriverManager.getConnection(url, userId, passWord);
1133 } catch (Exception e) {
1134 e.printStackTrace();
1135 }
1136 return conn;
1137 }
1138
1139 /**
1140 *gets the connection to oracle development database
1141 *
1142 * development database is a oracle database; java uses oracle odbc driver to
1143 * connect the database. This development database is accessed using database
1144 * service name with @. The service name in this script is pulsed (@pulsed ).
1145 * this service name is defined in tnsnames.ora of oracle_home/network/admin folder.
1146 * Oracle reads this file to get the connection specification like ip addBk.address of the machine
1147 * where the database exist, protocol, tcp port information in order to connect the
1148 * database.
1149 *
1150 *@param userId - oracle database userid entered by user
1151 *@param passWord - oracle database password
1152 *
1153 *@return connection - returns connection
1154 */
1155
1156 public java.sql.Connection connectDevelopment(String userId, String passWord) {
1157 conn = null;
1158 String url = "jdbc:odbc:pulsed"; // work on pulsed
1159 String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // work on pulsed
1160 try {
1161
1162 Class.forName(driver);
1163 conn = java.sql.DriverManager.getConnection(url, userId, passWord);
1164 } catch (ClassNotFoundException e) {
1165 e.printStackTrace();
1166 } catch (java.sql.SQLException e) {
1167 e.printStackTrace();
1168 }
1169 return conn;
1170 }
1171
1172 /**
1173 *gets the connection to other databases
1174 *
1175 * other databases like foxpro could be connected using odbc driver.
1176 *
1177 *@param userId - oracle database userid entered by user
1178 *@param passWord - oracle database password
1179 *
1180 *@return connection - returns connection
1181
1182 */
1183
1184 public java.sql.Connection connectOdbc(String userId, String passWord, String serviceName) {
1185 conn = null;
1186 String url = "jdbc:odbc:" + serviceName; // work on pulsed
1187 String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // work on pulsed
1188 try {
1189
1190 Class.forName(driver);
1191 conn = java.sql.DriverManager.getConnection(url, userId, passWord);
1192 } catch (ClassNotFoundException e) {
1193 e.printStackTrace();
1194 } catch (java.sql.SQLException e) {
1195 e.printStackTrace();
1196 }
1197 return conn;
1198 }
1199
1200 /**
1201 *exQuery method is executed when query button is pressed.
1202 *
1203 *once the tables from the database are populated into tablelist user could
1204 * select a table and press query button to get the data of the table in a table format
1205
1206 */
1207 public void exQuery() {
1208 recordsVector = new java.util.Vector(); // where the records are stored
1209 tableName = (String) tableList.getSelectedValue(); // selected table name
1210 if (tableName == null) {
1211 javax.swing.JOptionPane.showMessageDialog(
1212 null,
1213 "Please select a table to query data ",
1214 "Error Message ",
1215 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1216
1217 }
1218
1219 initArrays(); // init all arrays
1220 // puts records into a vector
1221 recordsVector = getRecords(query(conn,
1222 "SELECT * FROM "
1223 + tableName));
1224 // get the total no of columns in the table
1225 cols = getColumnCount(
1226 getResultSetMetaData(
1227 query(conn,
1228 "SELECT * FROM "
1229 + tableName)));
1230 // gets the resultsetmetaData of the query
1231 tableRsmd = getResultSetMetaData(
1232 query(conn,
1233 "SELECT * FROM "
1234 + tableName));
1235 // gets no of rows in the table
1236 rows = getNumberofRows(recordsVector);
1237
1238 columnTypes = new String[cols];
1239 // gets the column types and stores in String array
1240 columnTypes = getColumnTypeNames(
1241 getResultSetMetaData(
1242 query(conn,
1243 "SELECT * FROM "
1244 + tableName)));
1245 // formats the data into a table
1246 formatTable(rows, cols);
1247
1248 javax.swing.JOptionPane.showMessageDialog(
1249 null,
1250 null,
1251 "Query is executed",
1252 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1253 }
1254
1255 /**
1256 *initArrays, is called to initialize array and cells
1257 *
1258 */
1259
1260 public void initArrays() {
1261 newRow = null;
1262 inserted = false;
1263 browsePanel.remove(innerPanel);
1264 show();
1265 }
1266
1267 /**
1268 *formats the data in a presentable format Table.
1269 *
1270 *This method is called when the user select a table and press query button.
1271 *
1272 *This method takes no of rows and columns in the table through parameter and
1273 * creates JTextFields and populates value in them. Every JTextField is associated
1274 * with gui.mouse.m2.mouse handler, and action listener. Mouse Handler would keep track of the
1275 * current row and column of the cell the gui.mouse.m2.mouse pointer is pressed. Action listener is
1276 * process the update statement in the event of enter is pressed after a value is changed.
1277 *
1278 *@param rows - row no of rows
1279 *@param cols - no of columns in a row
1280 */
1281
1282 public void formatTable(int rows, int cols) {
1283 int r = 0;
1284 int c = 0;
1285 int row = 0;
1286 browsePanel.removeAll();
1287 browsePanel.repaint();
1288 innerPanel = new javax.swing.JPanel();
1289 browsePanel.setLayout(new java.awt.FlowLayout());
1290 innerPanel.setLayout(new java.awt.GridLayout(0, cols));
1291 newRecordsPanel.setLayout(new java.awt.GridLayout(0, cols));
1292 javax.swing.JTextField tf [] = new javax.swing.JTextField[rows * cols];
1293
1294
1295 for (r = 0; r < rows; r++)
1296 for (c = 0; c < cols; c++) {
1297 System.out.println("row " + r + " col" + c);
1298 tf[row] = new javax.swing.JTextField(10);
1299 tf[row].addActionListener(this);
1300 tf[row].addMouseListener(new rdbms.RdbmsUtil.MouseHandler(r, c));
1301 addComponent(innerPanel, tf[row]);
1302 row++;
1303 }
1304 browsePanel.remove(innerPanel);
1305 browsePanel.add(innerPanel, 0);
1306 fillData(tf, recordsVector, rows, cols);
1307 show();
1308
1309 }
1310
1311 /**
1312 *getOldValue method is used to get the old value of the cell
1313 *
1314 *This method is called when the user changes the value of any cell, and the old
1315 * and new values are shown in a dialog box
1316
1317 *@return String - old value of the cell
1318 */
1319 public String getOldValue(int row, int col) {
1320 String rowArr [];
1321
1322 rowArr = (String[]) recordsVector.elementAt(row);
1323 return rowArr[col];
1324
1325 }
1326
1327 /**
1328 *generates update statement and keeps in an array
1329 *
1330 *method generateUpdateStmt is used when a value in a cell is changed and pressed
1331 * enter key to confirm the change is done. This method gets the current row and column
1332 * that is set by the gui.mouse.m2.mouse handler, gets the old value from the vector and current value
1333 * and generate the update statment
1334 *
1335 *@param row - currect row
1336 *@param col - current column
1337 *@param newValue new Value
1338 */
1339 public void generateUpdateStmt(int row, int col, String newValue) {
1340 String rowArr [];
1341
1342 String sql = null;
1343 rowArr = (String[]) recordsVector.elementAt(row);
1344 // init update statement vector in case of first update
1345 if (!updated) {
1346 updateStmtVector = new java.util.Vector();
1347 updated = true;
1348 }
1349
1350 sql = "update " +
1351 tableName +
1352 " set " + columnNames[col] + " = ";
1353 if (columnTypes[col].equals("varchar2")) {
1354 sql += "\'";
1355 sql += newValue;
1356 sql += "\'";
1357 } else if (columnTypes[col].equals("number")) {
1358 sql += newValue;
1359 } else if (columnTypes[col].equals("date")) {
1360 try {
1361 sql += "to_date('";
1362 sql += newValue.substring(0, 10);
1363 sql += "\','YYYY-MM-DD')";
1364 } catch (StringIndexOutOfBoundsException e) {
1365 javax.swing.JOptionPane.showMessageDialog(
1366 null,
1367 "Invalid date format!!!\n Valid format is [yyyy-mm-dd]",
1368 "Error Message ",
1369 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1370 }
1371 } else {
1372 sql += "\'";
1373 sql += newValue;
1374 sql += "\'";
1375 }
1376 sql += " where ";
1377 for (int c = 0; c < cols; c++) {
1378 if (c > 0) {
1379 sql += " and ";
1380 }
1381 if (columnTypes[c].equals("date")) {
1382 sql += "trunc("; // oracle function to truncate time in date column
1383 sql += columnNames[c] + ")";
1384 } else {
1385 sql += columnNames[c];
1386 }
1387
1388 sql += formatColValue(c, rowArr[c]);
1389 }
1390 updateStmtVector.addElement(sql);
1391 }
1392
1393 /**
1394 *formatColValue method is used to formats the sql script
1395 *
1396 *The method formatColValue is used to formats the sql script as the oracle
1397 * database requires the column to be formatted using specific oracle function
1398 * to_date to format a date type before update.
1399 *
1400 *@param col column index which is to be formatted
1401 *@param value of the column
1402 */
1403
1404 public String formatColValue(int col, String value) {
1405 String fmtValue = null;
1406 System.out.println("col # " + col);
1407 if (value == null) {
1408 fmtValue = "is ";
1409 fmtValue += value;
1410 } else if (columnTypes[col].equals("varchar2")) {
1411 fmtValue = " = ";
1412 fmtValue += "\'";
1413 fmtValue += value;
1414 fmtValue += "\'";
1415 } else if (columnTypes[col].equals("number")) {
1416 fmtValue = " = ";
1417 fmtValue += value;
1418 } else if (columnTypes[col].equals("date")) {
1419 try {
1420 fmtValue = " = ";
1421 fmtValue += "to_date('";
1422 fmtValue += value.substring(0, 10);
1423 fmtValue += "\','YYYY-MM-DD')";
1424 } catch (StringIndexOutOfBoundsException e) {
1425 javax.swing.JOptionPane.showMessageDialog(
1426 null,
1427 "Invalid date format!!!\n Valid format is [yyyy-mm-dd]",
1428 "Error Message ",
1429 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1430 }
1431
1432 } else {
1433 fmtValue = " = ";
1434 fmtValue += "\'";
1435 fmtValue += value;
1436 fmtValue += "\'";
1437 }
1438 System.out.println("column type" + columnTypes[col]);
1439 System.out.println("fmt value " + fmtValue);
1440 return fmtValue;
1441 }
1442
1443 /**
1444 *fillData method is called to fill up the data into JTextFields that forms a table.
1445 *
1446 *filldata method gets data in a vector, and the no of rows and columns. It reads the data
1447 * from the vector which has elements of String Arrays. This methods reads the data from
1448 *the String array and populates into JTextField .
1449 *The column names row would be set to not editable
1450 *
1451
1452 */
1453 public void fillData(javax.swing.JTextField[] tf, java.util.Vector v, int rows, int cols) {
1454 String rowArr [];
1455 columnNames = new String[cols];
1456 int i = 0;
1457 for (int r = 0; r < rows; r++) {
1458 rowArr = (String[]) v.elementAt(r);
1459 for (int c = 0; c < cols; c++) {
1460 tf[i].setText(rowArr[c]);
1461 if (r == 0) {
1462 columnNames[i] = tf[i].getText();
1463 tf[i].setEditable(false);
1464 }
1465 i++;
1466
1467 }
1468 }
1469 }
1470
1471 /**
1472 *the method exUpdate is called when the update button is pressed.
1473 *
1474 *The exUpdate method gets the update statement (DML) from a updateStmtVector and
1475 * reads one by one and executes it.
1476 * once all update statements are executed it refreshes the database.
1477 */
1478
1479 public void exUpdate() {
1480 if (updated) {
1481 String updStmt = null;
1482 for (int i = 0; i < updateStmtVector.size(); i++) {
1483 updStmt = (String) updateStmtVector.elementAt(i);
1484 System.out.println("script " + updStmt);
1485 modifyDatabase(conn, updStmt);
1486 }
1487 exQuery();
1488 } else {
1489 javax.swing.JOptionPane.showMessageDialog(
1490 null,
1491 "No update statement to execute ...",
1492 "Message",
1493 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1494
1495 }
1496
1497 }
1498
1499 /**
1500 *the method exDelete is called when the delete button is pressed.
1501 *
1502 *The exDelete method get the current row and column set by the gui.mouse.m2.mouse handler and
1503 * deletes the row and refreshes the database after deleteion.
1504 */
1505
1506 public void exDelete() {
1507 String rowArr [];
1508 String sql = null;
1509 rowArr = (String[]) recordsVector.elementAt(currentRow);
1510 sql = "delete from " + tableName;
1511 sql += " where ";
1512 for (int c = 0; c < cols; c++) {
1513 if (c > 0) {
1514 sql += " and ";
1515 }
1516 if (columnTypes[c].equals("date")) {
1517 sql += "trunc(";
1518 sql += columnNames[c] + ")";
1519 } else {
1520 sql += columnNames[c];
1521 }
1522
1523 sql += formatColValue(c, rowArr[c]);
1524 }
1525
1526 modifyDatabase(conn, sql);
1527 exQuery();
1528 }
1529
1530 /**
1531 *the method exInsert is called when the insert button is pressed.
1532 *
1533 *The exInsert method get the no of columns in a row as a parameter and inserts JTextfields
1534 *into newRow Array of JTextFields
1535 *
1536 *@param cols - no of columns in a row
1537 */
1538
1539 public void exInsert(int cols) {
1540 if (!inserted) {
1541 newRow = new javax.swing.JTextField[cols];
1542 for (int c = 0; c < cols; c++) {
1543 newRow[c] = new javax.swing.JTextField(10);
1544 if (columnTypes[c].equals("number")) {
1545 newRow[c].setText("0");
1546 }
1547 addComponent(innerPanel, newRow[c]);
1548 }
1549 inserted = true;
1550 } else {
1551 System.out.println("cols " + cols);
1552 addNewRow(newRow, cols);
1553 }
1554 show();
1555 }
1556
1557 /**
1558 *the method addNewRow is called to include no of JTextfields into an old array
1559 * of JTextFields
1560 *
1561 *This method is called once the insert button is pressed
1562
1563 */
1564 public void addNewRow(javax.swing.JTextField[] oldArr, int cols) {
1565 System.out.println("old length " + oldArr.length);
1566 if (oldArr.length > 0) {
1567 javax.swing.JTextField newArr [] = new javax.swing.JTextField[cols + oldArr.length];
1568 copyArray(oldArr, newArr);
1569 newRow = new javax.swing.JTextField[newArr.length];
1570 copyArray(newArr, newRow);
1571 } else {
1572 javax.swing.JTextField newRow [] = new javax.swing.JTextField[cols];
1573 for (int c = 0; c < cols; c++) {
1574 newRow[c] = new javax.swing.JTextField(10);
1575 if (columnTypes[c].equals("number")) {
1576 newRow[c].setText("0");
1577 }
1578 }
1579
1580 }
1581
1582 for (int c = 0; c < newRow.length; c++) {
1583 addComponent(innerPanel, newRow[c]);
1584 }
1585 show();
1586
1587 }
1588
1589 /**
1590 *the method copyArray is used to copy the value of one array to another
1591 *
1592 */
1593 public void copyArray(javax.swing.JTextField[] oldArr, javax.swing.JTextField[] newArr) {
1594
1595 for (int c = 0; c < oldArr.length; c++)
1596 newArr[c] = oldArr[c];
1597
1598 for (int c = 0; c < newArr.length - oldArr.length; c++) {
1599 newArr[oldArr.length + c] = new javax.swing.JTextField(10);
1600 if (columnTypes[c].equals("number")) {
1601 newArr[oldArr.length + c].setText("0");
1602 }
1603 }
1604
1605 }
1606
1607 /**
1608 *saves the changes to database and refreshes it
1609 *
1610 *if the inserted flag is true then reads new records from newRow Array
1611 * formats insert statement and executes it.
1612 */
1613 public void exSave() {
1614 String sql;
1615 int totCols = newRow.length;
1616 int rowCount = 0;
1617 if (inserted) {
1618 for (int r = 0; r < (totCols / cols); r++) {
1619 sql = "insert into " +
1620 tableName +
1621 " values (";
1622 for (int c = 0; c < cols; c++) {
1623 System.out.println("type " + columnTypes[c]);
1624 if (columnTypes[c].equals("varchar2")) {
1625 sql += "\'";
1626 sql += newRow[rowCount].getText();
1627 sql += "\'";
1628 } else if (columnTypes[c].equals("number")) {
1629 if (newRow[rowCount].getText() != "") {
1630 sql += newRow[rowCount].getText();
1631 } else {
1632 sql += "0";
1633 }
1634
1635 } else if (columnTypes[c].equals("date")) {
1636 if (newRow[rowCount].getText().length() >= 10) {
1637 try {
1638 sql += "to_date('";
1639 sql += newRow[rowCount].getText().substring(0, 10);
1640 sql += "\','YYYY-MM-DD')";
1641 } catch (StringIndexOutOfBoundsException e) {
1642 javax.swing.JOptionPane.showMessageDialog(
1643 null,
1644 "Invalid date format!!!\n Valid format is [yyyy-mm-dd]",
1645 "Error Message ",
1646 javax.swing.JOptionPane.INFORMATION_MESSAGE);
1647
1648 }
1649 } else {
1650 sql += "null";
1651 }
1652 } else {
1653 sql += "\'";
1654 sql += newRow[rowCount].getText();
1655 sql += "\'";
1656 System.out.println(sql);
1657 }
1658
1659 if (c != cols - 1) {
1660 sql += ",";
1661 }
1662 rowCount++;
1663 }
1664 sql += ")";
1665 System.out.println("script " + sql);
1666
1667 modifyDatabase(conn, sql);
1668 }
1669 }
1670 // executes query to refreshes the table after update or delete or insert
1671 exQuery();
1672 }
1673
1674 /**
1675 *The method addComponent is called to include component into a panel.
1676
1677 */
1678
1679 private void addComponent(
1680 javax.swing.JPanel panel,
1681 java.awt.Component c) {
1682 panel.add(c);
1683 }
1684
1685 /**
1686 *The method addComponent is called to include component into a panel.
1687 *@param panel - panel name where the component is to be added
1688 *@param c - the component name that is to be added
1689 *@param row - row of the grid bag layout
1690 *@param col - column of the grid bag layout
1691 */
1692
1693
1694 // action performed is implemented
1695 /**
1696 *This action perfomed method is called when the user press enter key
1697 *after changing a value in any of the cell. If the value is changed
1698 *then an update statment is generated and kept in a vector which is executed later
1699 * when the save button is pressed.
1700 *
1701
1702 */
1703 public void actionPerformed(java.awt.event.ActionEvent e) {
1704 String evt = e.getActionCommand();
1705 String oldValue;
1706 String newValue;
1707 if (e.getSource() instanceof javax.swing.JTextField) {
1708 newValue = e.getActionCommand();
1709 oldValue = getOldValue(currentRow, currentCol);
1710 if (!oldValue.equals(newValue)) {
1711 generateUpdateStmt(currentRow, currentCol, newValue);
1712 }
1713 javax.swing.JOptionPane.showMessageDialog(null,
1714 "old Value " +
1715 oldValue +
1716 " New Value " + evt);
1717 }
1718 }
1719
1720 /**
1721 *main entry method which instantiates RdbmsUtil class
1722 */
1723
1724 public static void main(String args []) {
1725
1726 final RdbmsUtil ru = new RdbmsUtil();
1727
1728 ru.addWindowListener(
1729 new java.awt.event.WindowAdapter() {
1730 public void windowClosing(WindowEvent e) {
1731 ru.shutDown();
1732 System.exit(0);
1733 }
1734 }
1735 );
1736 }
1737
1738 /**class gui.mouse.m2.mouse handler is used to capture the current row and current column
1739 * of the cell where the gui.mouse.m2.mouse pointer is focussed
1740 *
1741 *This is used later to process the current row and column
1742 */
1743
1744 class MouseHandler
1745 extends java.awt.event.MouseAdapter
1746 implements java.awt.event.MouseMotionListener {
1747 // cell co-ordinates
1748 int row;
1749 int col;
1750
1751 // constructor takes the row and column
1752 public MouseHandler(int r, int c) {
1753 row = r;
1754 col = c;
1755 }
1756
1757 /**
1758 *sets the current row and column in a global variable
1759 *
1760 *the mousePressed event sets the currentRow and currentCol variables when the gui.mouse.m2.mouse is clicked on the cell
1761 *this current row and column variables are used to identify the current row and process it.
1762 */
1763 public void mousePressed(java.awt.event.MouseEvent e) {
1764 setCurrentRowCol(row, col);
1765 // setColumnColor(row,col);
1766 }
1767
1768 public void mouseDoubleClicked(java.awt.event.MouseEvent e) {
1769 setCurrentRowCol(row, col);
1770 // setRowColor(row,col);
1771
1772 }
1773
1774 public void mouseMoved(java.awt.event.MouseEvent e) {
1775 }
1776
1777 public void mouseReleased(java.awt.event.MouseEvent e) {
1778 }
1779
1780 public void mouseDragged(java.awt.event.MouseEvent e) {
1781 }
1782 }
1783
1784 }
1785