CHS2525 Advanced Databases

Assessed Coursework 2002-2003

Waq Hussain C8532263


Contents

Task 1 – Construction and use of an object-based interface to a relational database        

1.1- i)  Modify the application to display foreign keys.        

ii)        Modify the application with an Update button.        

1.2        Testing        

1.3         Evaluation of the modifications.        

JdbcCW7 – Code        

Task 2 – Investigation of object-relational databases        

2.1         Identify, read and summarise three sources of information.        

Object-Relational DBMSs: Tracking the Next Great Wave        

DBMS April 1998 – Modelling Object/Relational Databases        

Introduction to Database Systems        

2.2         Conclusion        


Task 1 – Construction and use of an object-based interface to a relational database

1.1- i)  Modify the application to display foreign keys.

To do this I created a new method called findFKConstraints().  The method is very similar to the existing method findPKConstraints(), however I have made the method change the text fields and labels of foreign key fields to red text.  To do this I changed the select query so that the column numbers were found as opposed to the position.  I needed to do this so that I knew which row of the editFields vector and the columntypes vector to change to red text.  Also if any foreign keys are found this method displays a label in the window that says ***Red text indicates a Foreign Key***.  

ii)        Modify the application with an Update button.

I decided that I would just have one update button that was only enabled when a text fields data had been modified.  Once the button has been enabled, and clicked, it runs a new method called update().

This method compares the values in each text field with the corresponding data in the rowData vector.  If there are any differences, then the method attempts to perform an update query.  The update query is created by obtaining data from the findPKConstraint() method.  i.e. the where clause is determined by any primary keys the table may have.  If the update query is successful, the data in the rowData vector is also updated so that if the user moves between rows the updated value is displayed.   If the update query is not successful a message box is displayed and the text field data is reverted back to its original value.  Finally the update method makes the update button inactive.

 


1.2        Testing

I fully tested my modifications.

I opened all the tables in the manufacturing database and tried to update any non-primary key fields.  They all appeared to work except for the following :-

Fields of type ‘date’ returned a value in the format ‘yyyy-mm-dd 00:00:00’.  However in order to Update a date field the format needs to be  ‘dd-mmm-yy’ e.g. 28-feb-02.  I decided that if I changed the format displayed to the ‘dd-mmm-yy’ format the user is more likely to use this format for any edits and also a message informing the user of the correct format.  In order to do this I created a method called DataFormat() to change the format of the ResultsSets value before adding it to the rows vector in the populateData () method.

Fields that have been created with type float(6,2) although the update query works, if the user tries to enters a number with more than two decimal places, rounding will take place.  e.g. if the user enters 9.999, the field will be updated to 10.  All the select statements I tried, only return the data types as number, whether it is an integer or a float. So I didn’t make any modifications for this problem.

When editing fields that are foreign key constraints (with red text), the update only worked if the value matched a field in the corresponding table.  e.g. Updating the companyid in the salesorder table, only works if the companyid is either a31, a41, p45 or s92.

1.3         Evaluation of the modifications.

Apart from the float types update described above all the modifications have been successful.

Other modifications made that are not described above are that the  Previous button is initially disabled.  If the rows vector has more than one row, the Next button is enabled, otherwise initailly it too is disabled.

Within the showRow() method, changes have been made so that the Next and Previous buttons are only enabled when there is a next or previous row.

JdbcCWc9953547 – Code

Changes I have made from the original are in italic.

import java.io.*;

import java.sql.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import oracle.jdbc.driver.*;

                        //import Date libraries for the Format Date method to work

import java.text.SimpleDateFormat;

import java.util.Date;

/**

 *

 * class JdbcCW

 *

 * demonstration of Java client connecting to an Oracle DBMS

 *

 * @author Waq Hussain

 * @date   December 2002

 */

public class JdbcCW extends JFrame{

  static String url = "jdbc:oracle:thin:@mercury:1526:ora8i";

  static String initialquery =  "select table_name from user_tables";

  private String user;

  private String pass;

  private Connection conn;

  private Statement stmt;

  private ResultSet results;

 

  private JComboBox tableChooser;

  private JButton editButton;

  private JButton exitButton;

  private JPanel panel1;

  private JPanel panel2;

  private String selectedTable;

  private Container cp;

  public static void main(String[] args) throws SQLException {

    JdbcCWc9953547 demo = new JdbcCWc9953547();

    demo.show();

  }

  /**

   *   JdbcCWc9953547 constructor method

   */

  public JdbcCWc9953547() throws SQLException {  

    super("Julie Drury - Java/Oracle coursework");

    addWindowListener (new WindowAdapter() {

                public void windowClosing (WindowEvent evt) {

                    System.exit(0);

                }

            }

        );

         

    try {

      user =

         JOptionPane.showInputDialog("enter oracle username eg ops$c9999999");

      pass = JOptionPane.showInputDialog("enter oracle password eg 29feb80");

      if ( user.length() == 0 || pass.length() == 0)

         throw new Exception("no user name and/or password");

      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  

      conn = DriverManager.getConnection(url, user, pass);

         

      if (conn != null)

        {System.out.println("Connected");}

        else

        {System.out.println("Not Connected");}

    }  

    catch (Exception e ) {

      e.printStackTrace();

      System.exit(0);          // problems - abandon ship

    }  

 

    //  now we can access tables in the database

 

    stmt = conn.createStatement();

    results = stmt.executeQuery(initialquery);

    boolean anyRecords = results.next();

    if ( ! anyRecords ) {

      JOptionPane.showMessageDialog (this, "No Tables in the database");

      System.exit(0);

    }

 

   tableChooser = new JComboBox();        

   do            

     tableChooser.addItem(results.getString(1));      

   while (results.next() ) ;

   selectedTable =  (String) tableChooser.getSelectedItem();

   

   tableChooser.addActionListener (new ActionListener () {

      public void actionPerformed (ActionEvent evt) {

        changeTable();

      }

     }

    );

   editButton = new JButton("Edit");

   editButton.addActionListener(new ActionListener () {

     public void actionPerformed(ActionEvent evt) {

         try

         { runEdit();}

         catch (Exception e)

         {

         System.out.println(e);

         }

     }

    }

   );

   exitButton = new JButton("Exit");

   exitButton.addActionListener(new ActionListener () {

     public void actionPerformed(ActionEvent evt) {

       System.exit(0);

     }

    }

   );

   panel1 = new JPanel();  // panels have flow layout

   JLabel label1 = new JLabel("Choose Table");

   panel1.add(label1);

   panel1.add(tableChooser);

 

   panel2 = new JPanel();

   panel2.add(editButton);

   panel2.add(exitButton);

   cp = getContentPane();

   cp.add(panel1,BorderLayout.CENTER);

   cp.add(panel2,BorderLayout.SOUTH);

   setSize(300,200);

   setLocation(100,100);

    }

  private void changeTable() {

    selectedTable = (String) tableChooser.getSelectedItem();

  }

  /**

   * method runEdit runs a query to determine the structure of the

   * selected table in order to customise the table editor window

   *

   */

  private void runEdit() throws Exception{

      String query = "select column_name,data_type from user_tab_columns " +

          "where table_name = '" + selectedTable + "'";

      try {

          results = stmt.executeQuery(query);

      } catch (java.sql.SQLException e) {

          System.out.println("SQL Exception on query " + query);

          return;

      }

      JdbcEdit tableEditor = new JdbcEdit(this,selectedTable,results);

      tableEditor.show();

 }

   public ResultSet makeQuery(String query) {

        ResultSet results;

        try {

            results = stmt.executeQuery(query);

        } catch (SQLException e) {

         

            System.out.println("Query Failed " + query);

            return null;

        }

        return results;

    }

} // end class JdbcCWc9953547

/**

 * class JdbcEdit

 *

 * oracle table editing dialog window

Join now!

 *

 * @author Waqhussain

 * @date   December 2002

 */

class JdbcEdit extends JDialog {

    private JdbcCW parent;

    private Container cp;

    private Vector columnNames;

    private Vector dataTypes;

    private Vector editFields;

    private Vector rows;

                

                private String table;

                

                private Vector PKresults;

                        

    private int noOfColumns;

    private int currentRow = 0;

   

                                   //jd. have declared buttons and labels outside of constructor

                                   //jd. so I can change there status within methods.

                                   

    private JButton updateButton = new ...

This is a preview of the whole essay