com.javaranch.db
Class Jenny

java.lang.Object
  |
  +--com.javaranch.db.Jenny

public class Jenny
extends java.lang.Object

Jenny the db code generator. This program reads a database and generates java source which provides strongly typed access to the database. In my opinion, there are two major problems with JDBC:

For each table and view in a given database, Jenny will create a java source class file. Each of these classes will provide a collection of general purpose methods for working with the table/view. They will also provide a list of all the column names, the table/view name and an inner class called Row that can be used to manipulate a single row in the table. Many tables have a primary key that is a non-nullable integer called "tablenameID" or sometimes "ID". If Jenny finds this, she will add extra methods to the generated source for working with the primary key. Some of my goals: Each generated class will provide some basic methods such as these:

   Row getRow()
   Row getRow( String column , String searchText )
   DBResults search( String column , String searchText , String[] dataColumns )
   Row[] getRow( String column , String searchText )
   Row[] getAllRows()
   DBResults getAllRows( String[] dataColumns )
   void update( String column , String searchText , Map data )
   void delete( String column , String searchText )
   void insert( Map data )

 
If an ID field is found, some methods like these will also be added:

   Row getRow( int id )
   void delete( int id )

 
Every class will have an inner class called Row that can provide strong type checking for every field (column) as well as methods like:

    void update()
    void delete()
    void insert()

 
The strong type checking for Row is provided by getters and setters. Suppose you have a table called Employee. Jenny will generate a class called EmployeeTable that will contain a Row class that might have the following methods:

    int getEmployeeID()
    void setEmployeeID( int employeeID )
    String getLastName()
    void setLastName( String lastName )

 
Here's a sample of a business logic method that uses a Jenny generated class.

    // pass in an employee ID and get back the number of tax exemptions that the employee claims
    private int getEmployeeExemptions( int employeeID )
    {
        return EmployeeTable.getRow( employeeID ).getExemptions();
    }

 
This same code using plain JDBC could be 10 to 40 lines long depending on how it would be implemented. You would need to get a connection, create a statement, build your sql string, execute your statement, wade through the result set, and each of these things need try/catch/finally blocks! Don't forget to close your connection, statement and result set!

Using Jenny

From the command line: - - - - - - - - - - - - - - - - -

Copyright (c) 2003 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton, Marilyn de Queiroz

Constructor Summary
Jenny()
           
 
Method Summary
 com.javaranch.db.Jenny.TableProcessor getTableProcessor(java.sql.DatabaseMetaData metaData, java.lang.String tableName, java.lang.String suffix, java.lang.String idColumnName)
           
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Jenny

public Jenny()
      throws java.lang.Exception
Method Detail

getTableProcessor

public com.javaranch.db.Jenny.TableProcessor getTableProcessor(java.sql.DatabaseMetaData metaData,
                                                               java.lang.String tableName,
                                                               java.lang.String suffix,
                                                               java.lang.String idColumnName)
                                                        throws java.lang.Exception
java.lang.Exception

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
java.lang.Exception


Copyright ©2004 Paul Wheaton All Rights Reserved