Back to index

wims  3.65+svn20090927
Public Member Functions | Package Functions | Package Attributes | Private Member Functions | Private Attributes | Static Private Attributes
SharpTools.Database Class Reference

This contains database operations on the spreadsheet table. More...

Collaboration diagram for SharpTools.Database:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void connectDb ()

Package Functions

 Database (SharpTools sharp)
 constructor

Package Attributes

final ImageIcon connectedIcon = new ImageIcon(getClass().getResource("/images/database32.gif"))

Private Member Functions

String checkForWarning (SQLWarning warn) throws SQLException
String selectDbTable (DatabaseMetaData dma) throws SQLException

Private Attributes

SharpTools sharp
JTable table
SharpTableModel tableModel

Static Private Attributes

final static ImageIcon dbErrorIcon = null

Detailed Description

This contains database operations on the spreadsheet table.

Author:
Shiraz Kanga
Version:
Revision:
1.7

Definition at line 44 of file Database.java.


Constructor & Destructor Documentation

SharpTools.Database.Database ( SharpTools  sharp) [inline, package]

constructor

Parameters:
sharpthe GUI object

Definition at line 61 of file Database.java.


Member Function Documentation

String SharpTools.Database.checkForWarning ( SQLWarning  warn) throws SQLException [inline, private]

Definition at line 167 of file Database.java.

                           {
       StringBuffer textBuf = new StringBuffer ();
       
       if (warn != null) {
           textBuf.append ("Warning:\n\n");
           while (warn != null) {
              textBuf.append ("Message:  " + warn.getMessage () + "\n");
              textBuf.append ("SQLState: " + warn.getSQLState () + "\n");
              textBuf.append ("Vendor:   " + warn.getErrorCode () + "\n");
              warn = warn.getNextWarning ();
           }
       }
       return textBuf.toString ();
    }

Here is the caller graph for this function:

void SharpTools.Database.connectDb ( ) [inline]

Definition at line 68 of file Database.java.

    {
       String dbUsername = null;
       String dbPassword = null;
       String dbUrl = null;
       String dbDriver = null;
       String dbTable = null;
       String connectName = null;
       
       boolean lockTable;
       boolean verifySave;
       boolean newConnection = false;
       
       StringBuffer textBuf = new StringBuffer ();
       
       ConnectDialog connectDialog = new ConnectDialog (sharp);
       connectDialog.setVisible (true);
       if (connectDialog.isCancelled())
           return;
       
       Connection dbConnection = connectDialog.getConnection();
       /*
         connectName = connectDialog.getConnectName ();
         
         dbUsername = connectDialog.getDbUsername ();
         dbPassword = connectDialog.getDbPassword ();
         dbUrl = connectDialog.getDbUrl ();
         dbDriver = connectDialog.getDbDriver ();  
       */
       
       try {
           DatabaseMetaData dma = dbConnection.getMetaData ();
              
           SharpOptionPane.showMessageDialog (sharp, "Connected to database " + dma.getDatabaseProductName () + " v" + dma.getDatabaseProductVersion () + "\nusing driver " + dma.getDriverName () + " v" + dma.getDriverVersion (),
                                          "Connected", JOptionPane.INFORMATION_MESSAGE, connectedIcon);
           
           if ((dbTable == null) || dbTable.equals (""))
              dbTable = selectDbTable (dma);

           // need to check cancel - huaz
           if (dbTable == null)
              return;
           
           Statement stmt = dbConnection.createStatement ();
           ResultSet dbResults = stmt.executeQuery ("SELECT * FROM " + dbTable);
           
           String warnings = checkForWarning (dbConnection.getWarnings ());
           if ((warnings!=null) && !warnings.equals (""))
              SharpOptionPane.showMessageDialog (sharp, warnings,
                                             "Warning", JOptionPane.INFORMATION_MESSAGE, connectedIcon);
           
           // metadata can supply information about the schema
           ResultSetMetaData rsmd = dbResults.getMetaData ();
           int numCols = rsmd.getColumnCount ();
           
           // first print header labels from meta-data
           for (int i=1; i<=numCols; i++) {
              if (i != 1) textBuf.append ("\t");
              textBuf.append (rsmd.getColumnLabel (i));
           }
           textBuf.append ("\n");
           
           while (dbResults.next ()) {
              // for one row
              for (int j=1; j<=numCols; j++) {
                  if (j != 1) textBuf.append ("\t");
                  textBuf.append (dbResults.getString (j));
              }
              textBuf.append ("\n");
           }
           
           String text = textBuf.toString ();
           // create new table model
           CellPoint size = SharpTableModel.getSize (text);
           //      System.out.println (size);
           sharp.newTableModel (size.getRow (), size.getCol ());
           tableModel = sharp.getTableModel ();
           tableModel.fromString (text, 0, 0, new CellRange (1, size.getRow (), 1, size.getCol ()));
           
           // tableModel.setModified (false);
           // set new title for spreadsheet
           sharp.setTitle (dbTable + " - Sharp Tools Spreadsheet");
           
           // update recent files
           //addRecentFile (dbTable);         
           
           dbResults.close ();
           stmt.close ();
       }
       catch (SQLException e) {
           SharpOptionPane.showMessageDialog (sharp, "Unable to get data from the database.\n" + e.toString (),
                                          "ERROR", JOptionPane.INFORMATION_MESSAGE, dbErrorIcon);
           return;
       }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

String SharpTools.Database.selectDbTable ( DatabaseMetaData  dma) throws SQLException [inline, private]

Definition at line 183 of file Database.java.

                           {
       // JDBC exposes must meta data as ResultSets
       // Change the second parameter below to retrieve information
       // about a particular schema in the database
       ResultSet dbResults = dma.getTables (null, null, "%", null);
       Vector vec = new Vector ();
       
       /*
         Each table description row has the following columns:
         TABLE_CAT String => table catalog (may be null)
         TABLE_SCHEM String => table schema (may be null)
         TABLE_NAME String => table name
         TABLE_TYPE String => table type
         Common types are "TABLE", "VIEW", "SYSTEM TABLE"
         REMARKS String => explanatory comment on the table
         We are only want column 3 (table name)
       */
       
       while (dbResults.next ())
           vec.addElement (dbResults.getString (3));

       if (vec.size() == 0) {
           SharpOptionPane.showMessageDialog(sharp,
                                         "This database has no tables defined.",
                                         "Empty Database",
                                         JOptionPane.WARNING_MESSAGE);
           return null;
       }   
       
       Object[] possibleValues = new Object[vec.size ()];
       vec.copyInto (possibleValues);
       Object selectedValue = SharpOptionPane.showInputDialog(sharp,
                                                        "Please select the table you wish to load: ", "Select Table",
                                                        0,
                                                        /*selectTableIcon*/
                                                        connectedIcon,
                                                        possibleValues,
                                                        possibleValues[0]);
       if (selectedValue == null)
           return null;
       else
           return selectedValue.toString ();
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

final ImageIcon SharpTools.Database.connectedIcon = new ImageIcon(getClass().getResource("/images/database32.gif")) [package]

Definition at line 50 of file Database.java.

final static ImageIcon SharpTools.Database.dbErrorIcon = null [static, private]

Definition at line 54 of file Database.java.

Definition at line 47 of file Database.java.

JTable SharpTools.Database.table [private]

Definition at line 48 of file Database.java.

Definition at line 49 of file Database.java.


The documentation for this class was generated from the following file: