On-line log book


23 October, 97

JDBC

I have finally got JDBC to work. The main points are

  1. Run ODBC Administrator from Control Panel and set up the database as an ODBC data source.
    In my case, I was working with a Microsoft Access DB and I set it up as called Rincewind1
  2. Once that is done, it was a simple case of running Jexplorer and using the following parameters:

 

To set it up using Java code I used the following:

try {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");           //Use Jdbc-Odbc Driver
} catch (ClassNotFoundException e) {
  System.out.println("Exception registering driver");
  e.printStackTrace();
}
try {
  String url = "jdbc:odbc:Rincewind1";
  Connection con = DriverManager.getConnection(url);       // Make connection with DB
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * FROM Books"); // Execute Query
  while (rs.next()) {
    for (i=1;i<4;i++)
      System.out.print(rs.getString(i)+": ");              // Print out results
    System.out.println();
  }
} catch (Exception e) {
  e.printStackTrace();
}

This code prints out all the entries in the Books table of the database Rincewind1


28 October, 97

IBM DB2

There was no DB2 JDBC driver with the installation, and so I just created another ODBC data source out of the SAMPLE database which comes with DB2 and attached to that in the same way as above, using Jexplorer to have a look around it.

Looking at CDM ToolKit Data file

I created a data source in the same way that I had done previously, attached to CDMDAT32.MDB. When I than attached this data source to Jexplorer, it decided that this source was acually made up of three databases, which I do not actually remember attaching. It allowed me to look through all of the data in each of the three which was very suprising. It also showed up the fact that either MS-Access does not support Primary, Imported and Exported Keys in the same way as Jexplorer understands them, or else we have not set any up. Further investigation is probably needed


10 November, 97

Visual Age Java

Finally worked out how to use it. Tried out the two tutorials. One starts you from the ground up all in code, the other starts you at the beginning, showing you how to create a graphical application which uses built in Visual Age Java (VAJ) connection things to do all the work for you so that you do not have to write any code what so ever.

Finally managed to create an application which brings up a visually created form and reacts to open specific database, using specific driver. Had to Export class from VAJ though and run using command line Java VM for it to understand class sun.jdbc.odbc.JdbcOdbcDriver as do not know how to change VAJ's CLASSPATH variable or whatever it uses for this purpose.


13 November, 97

Structure for Template tables:

IBM DB2

I have been trying to add tables from CDM ToolKit into DB2. It is a very slow process for a number of reasons.


18th November, 97

CDM ToolKit Tables

I have now finished creating the Organisation, Personnel, ProjectDescription, ProjectDates and ProjectPersonnel tables within DB2. I have then imported all of the data into them from the text files.

Lotus Approach

I then started to play with Approach to edit the data and add more records. This was fine with the Organisation table, but all of the other 4 were, according to Approach, read only. This meant that it was impossible to do anything with them. The only difference, as far as I can see, is that the Organisation table was put in a table space when I had only created one container, and the others were created after I put an extra container in. I cannot see how this would cause a problem though. I also tried to create a table space which the system would handle the size of, but still to no avail.

Also, when I tried to access the PROJECTDESCRIPTION table, Approach crashes every single time. I have tried deleting it and recreating it, but it still crashes, saying that the 'DDE Server Window: Approach' has an instruction which is trying to access memory at some location which 'cannot be read'.


22nd November, 97

IBM DB2

Created Template tables TEMPLATE_TABLE and TEMPLATE_FIELD as specified above.

Approach

On viewing in Approach, I can access them without any trouble whatsoever. They are not read only, and changes can be made simply and easily. This is good, but I still do not understand what is wrong with the other tables, as they are still read only!

Visual Age Java

I will now try to create a Java Applet and put it in one of these template tables to see what can be done.

java.lang.ClassLoader seems to be the function to use to actually load and create an instance of the class once it is saved locally.


24th November, 97

Visual Age Java

The aim for today was to get classes to be dynamically loadable through the use of the java.lang.ClassLoader class.

At the moment, these two classes and one interface are loaded into VAJ and run fine, loading HelloApp without any trouble. The problem comes when I try to export these classes and run them using JDK 1.1. For some reason, even using the same filename, the file HelloApp.class is not found.

On compiling through JDK 1.1 as well, the method defineClass is apparently deprecated, but I think I'll just stick to VAJ for the moment. I have created the HelloApp class using the JDK though, so hopefully if I stay doing things like that for the moment there won't be a problem.


25th November, 97

Visual Age Java

Today I worked on finding out more about database metadata and Java AWT programming.

The idea was to take the application from yesterday, make it bring up a new window, place the list of tables for this database into one ListBox and when the user selected a table, the list of columns and their related sizes would pop up in another ListBox.

NB: All further database work will be done using DB2 and its related JDBC driver (COM.ibm.db2.jdbc.app.DB2Driver). All URLs to databases will be constructed using jdbc:db2:<name> and my sample DB URL is jdbc:db2:cdmdata. The only problem with this is that the driver is only found when the application is exported from VAJ and run from the command line which is a bit of an annoying process. I really need a way of being able to change whatever the VAJ CLASSPATH variable is.

The new window design was not too difficult. I created a new class within VAJ and used its Visual Composition part to create the window, then took the code it created and compacted it down.

Doing this I ended up with a window which looked like this:

 

It was just a case of using JDBC meta data code from [1]:

  DatabaseMetaData mdDB; ResultSet mrsDB;
  String tblName;
  String[] types = { "TABLE" };
  mdDB = conDB.getMetaData();
  mrsDB = mdDB.getTables(null, "%", "%", types);
  while (mrsDB.next())
    System.out.println(mrsDB.getString(3));
  mrsDB.close();
(where conDB is a Connection object which is set up as a connection to the database), and I had each table name printed out to the console.

Changing the System.out.println to lstTables.add (where lstTables is a java.awt.List) meant I had a list of the tables in the left hand list box.

 

Now it was a case of picking up the event of the user double clicking on the left hand list, or selecting an item and clicking the View Columns button so that I could display the columns and their sizes in the right hand list.

I had been using JDK v1.0 AWT event handling in my previous applications, but I decided to change it all to JDK v1.1 AWT event handling, which meant doing things in a different way.

In the past, all that was needed was to have some way of identifying the button or whatever the event would come from in some way, and when the action event was called, check to see if the argument was that button, and then do something. With this method, there were hundreds of events (such as mouse movement), continuously being passed to the program, and needing to be processed.

The new way of thinking, is that an object (eg a Window) can listen for events only from specified other objects (eg a button).

All that is needed is for the window to implement the ActionListener interface, and override the actionPerformed(ActionEvent evt) method; the button then needs to include a call to button.addActionListener(this) so that events from it are picked up.

To implement a window listener for when to call System.exit(0) or dispose() works in the same sort of way. (For more info, see page guide/awt/HowToUpgrade.html in [4]).

 

Putting all of this together, created the package ProjectTests, and the application looks like:

 


References

  1. Gary Cornell and Cay S. Horstmann, Core Java, Second Edition, Prentice Hall, 1997.

  2. Prashant Sridharan, Advanced Java Networking, Prentice Hall, 1997.

  3. Andreas Vogel and Keith Duddy, Java Programming with CORBA, Wiley Computer Publishing, 1997.

  4. Sun Microsystems, JDK 1.1.1 Documentation, 1997.

  5. Various Java sites, which can be accessed from
    http://www.ecs.soton.ac.uk/~tjg295/project/JavaLinks.html

  6. S. Ceri and G. Pelgatti, Distributed Databases: Principles and Systems, McGraw-Hill, New York, 1984.

  7. Mark Papiani, Alistair N. Dunlop and Anthony J. G. Hey, Automatically Generating World-Wide Web Interfaces to Relational Databases, University of Southampton, 29th November, 1996

  8. Bernard Van Haecke, JDBC Java Database Explorer, 1996


Theo Gray: contact me
http://www.ecs.soton.ac.uk/~tjg295