java hosting


Jenny the database code generator - Tutorial

Sample Code using Jenny generated files

Preparation    Using Jenny    A Jenny generated file

The code to ADD a video to a table and then GET the description field of that video in that table might look something like this:

BEFORE USING Jenny:

import java.sql.* ;

public class VideoLookup
{

    public static void main( String[] args )
    {
        try
        {
            Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
            try
            {
                Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/soup" );

                try
                {
                    Statement statement = con.createStatement();
                    String sql = "INSERT INTO VIDEOS (TITLE, STAR, TYPE, VHS, DVD, DESCRIPTION ) " +
                            "VALUES ( 'The Gods Must Be Crazy', 'a coke bottle', 'comedy', 'Y', 'N', " +
                            "'A bushman is introduced to civilization by a coke bottle.' );";

                    statement.executeUpdate( sql );
                    ResultSet rs = statement.executeQuery(
                            "SELECT * FROM VIDEOS WHERE TITLE=\"The Gods Must Be Crazy\";");

                    while ( rs.next() )
                    {
                        System.out.println( rs.getString( "DESCRIPTION" ) );
                    }

                    rs.close();
                    statement.close();
                }
                catch ( SQLException e )
                {
                    System.out.println( "JDBC error: " + e );
                }
                finally
                {
                    con.close();
                }

            }
            catch( SQLException e )
            {
                System.out.println( "could not get JDBC connection: " + e );
            }
        }
        catch( Exception e )
        {
            System.out.println( "could not load JDBC driver: " + e );
        }
    }

}

USING Jenny:

import java.util.* ;
import com.acmevideo.db.* ;

public class VideoLookup
{

    public static void main( String[] args ) throws Exception
    {
        VideosTable.Row row = VideosTable.getRow();
        row.setTitle("The Gods Must Be Crazy");
        row.setStar("a coke bottle");
        row.setType("comedy");
        row.setVhs("Y");
        row.setDvd("N");
        row.setDescription("A bushman is introduced to civilization by a coke bottle.");
        row.insert();

        VideosTable.Row row = VideosTable.getRow( VideosTable.titleColumnName , "The Gods Must Be Crazy" );
        System.out.println( row.getDescription() );
    }

}

Notice how this looks more like Java and less like SQL.




Page maintained by Marilyn de Queiroz