# CHAPTER 4: Example Program (Category 4)

Example presented in this chapter shows an application that creates a table `myTable`, inserts 3 records and retrieves these records.

In order to have an example `myDemo.java` running, user has to do the following steps:

1. Since `myDemo.java` is a client application which accesses an Empress database on the machine where Empress Connectivity (or ODBC) server is running, user has to modify the `getConnection` parameters in the `myDemo.java` accordingly. For example:

          "jdbc:empress://SERVER=workstn1;PORT=6322;DATABASE=testdb",
             "joe", "code123"

   The `getConnection` parameters that show `workstn1` as a machine where Empress ODBC server is running, `6322` is a port code of the server, `testdb` is an Empress database located on the server machine and accessible by the server, `joe` and `code123` are client login name and the password account present in the Empress server password file.

2. To compile the sample code , user has to invoke java compiler.

          javac myDemo.java

   This command will generate `myDemo.class`.

3. To execute the generated `myDemo.class`, user has to invoke the java interpreter, for example:

          java myDemo

   It should be noted that its assumed that CLASSPATH has be set to include $EMPRESSPATH/jdbc/classes_cat4/empressJDBC_cat4.jar.

Here is the code of `myDemo.java`:

       import java.sql.*;

       public class myDemo
       {
          private static Connection      TheConnection;
          private static Statement       SqlHandle;
          private static ResultSet       Results;
          private static ResultSetMetaData MResults;

          public static void main (String args[])
          {
             int count;
             int num_attrs;
             String name;
             String type_name;
             int type;

             try
             {
                Class.forName ("com.empress.jdbc4.empressDriver");

                TheConnection = DriverManager.getConnection (
                   "jdbc:empress://SERVER=emery;PORT=6322;DATABASE=testdb",
                   "joe", "code123");

                SqlHandle = TheConnection.createStatement ();

                SqlHandle.executeUpdate("CREATE myTable (a char,
                   b double precision, c date)");

                SqlHandle.executeUpdate("INSERT INTO myTable VALUES
                   ('blue', 2.34, 19980213)");

                SqlHandle.executeUpdate("INSERT INTO myTable VALUES
                   ('red', 3.45, 19980214)");

                SqlHandle.executeUpdate("INSERT INTO myTable VALUES
                   ('green', 4.56, 19980215)");

                Results = SqlHandle.executeQuery ("SELECT * FROM myTable");
                MResults = Results.getMetaData ();

                num_attrs = MResults.getColumnCount ();

                while (Results.next () == true)
                {
                   for (count=1; count<=num_attrs; count++)
                   {
                      name = MResults.getColumnName (count);
                      type = MResults.getColumnType (count);
                      type_name = MResults.getColumnTypeName (count);
                      System.out.print (name+"["+type_name+"]["+
                                        MResults.getPrecision (count)+"]["+
                                        MResults.getScale(count)+"]["+
                                        MResults.isNullable (count)+"]: ");

                      switch (type)
                      {
                         case Types.DOUBLE:
                            System.out.println ("(double)" +
                               Results.getDouble (count));
                            break;
                         case Types.DATE:
                            System.out.println ("(date)" +
                               Results.getDate (count));
                            break;
                         default:
                            System.out.println ("(string)" +
                               Results.getString (count));
                            break;
                       }
                   }
                }
             }
             catch (SQLException ex)
             {
                SQLException eptr;
                for (eptr = ex; eptr != null; eptr = eptr.getNextException())
                {
                   System.out.println ("Message: "+eptr.getMessage());
                }
                ex.printStackTrace ();
             }
             catch (Exception other)
             {
                other.printStackTrace ();
             }
          }
       }