java hosting


Jenny the database code generator - Tutorial

Using Jenny

Preparation    A Jenny generated file    Code Examples

Installing Jenny

  • If you do not yet have a database/table to work with, go to the Preparation page.
  • Download jenny.zip, and put it in your classpath, or in your IDE's classpath if you will be using Jenny in an IDE.

  • Define a jenny.properties file using this sample properties file as a template. Copy this file to your work directory and give it a more appropriate name. Then edit the file so it reflects how you want Jenny to generate your class files.

    For instance, your rental.properties file may look like this modified properties file.

Using Jenny

  • Run Jenny every time you change your database. From the command prompt type:
        java com.javaranch.jenny.dev.Jenny  db.properties 
      where db.properties is the properties file you wish to use for this database.

    Other ways to run Jenny include:
    From your IDE:
    From an icon on your Windows desktop:
    In a build script, Ant script, or batch file:

  • Now that you've created your "personalized" files, you are ready to use them.

    You should be able to see a Java source file for each of your tables in your database. Compile and use the generated source as if you wrote it yourself! For example:

    Let's say that you have a database named "rentals". Within the rentals database you have tables named "videos", "compactDisks", "booksOnTape". After you run Jenny, you should see a subdirectory named rentals (the subdirectory is defined by the properties file) which contains classes named "Videos", "CompactDisks", and "BooksOnTape". Each of these classes contains methods with the same signature as the methods in DBFacade, which have been overridden. Now you can create a method in your code like:

          // pass in an serial number and get back 
          // the title of the video that you are searching for.
          private String getVideoTitle( int serialNumber )
          {
              return VideosTable.getRow( serialNumber ).getTitle();
          }
          
    and you won't have to worry about getting the wrong thing back because someone changed the name of the column from "Title" to "Titles" or "TITLE" or having to change the code in the getTitle() method from
            out.print( rs.getString( "Title" ) );
          
    to
            out.print( rs.getString( "Titles" ) );
          
    everywhere you've used that column name.

    For more examples of how to use the files that Jenny generated, click here.

    A sample properties file.

    VideosTable.java file that was created from the "videos" table by Jenny

    An entire program that adds one record and then reads and shows that record.




Page maintained by Marilyn de Queiroz