001package bradleyross.opensource.jackcess;
002// import java.sql.Types;
003import com.healthmarketscience.jackcess.*;
004import bradleyross.opensource.jackcess.helpers;
005/** 
006* Reads an MDB file using the Microsoft Access 2000 format
007* and displays information concerning its layout and
008* contents.
009* @author Bradley Ross
010*/
011public class thirdTest 
012{
013        static void processTable(Table table) throws Exception
014        {
015                Cursor cursor;
016                // java.util.Iterator<java.util.Map<String,Object>> iterator = null;
017                java.util.Map<String,Object> row;
018                String indexedColumnName = null;
019                /*
020                ** List columns
021                */
022                System.out.println("There are " + Integer.toString(table.getRowCount()) + " rows");
023                System.out.println("List names of columns");
024                System.out.println("There are " + Integer.toString(table.getColumnCount()) + " columns");
025                java.util.List<Column> columnList = table.getColumns();
026                for (int i = 0; i < table.getColumnCount(); i++)
027                {
028                        Column column = columnList.get(i);
029                        System.out.print(
030                        Short.toString( column.getColumnNumber()) + " " 
031                        + columnList.get(i).getName() + " " +
032                        helpers.decodeDataType(column.getType()) + " ");
033                        if (i%3 ==2) 
034                                { System.out.println(""); }
035                        else
036                                { System.out.print("          "); }
037                }
038                java.util.List<Index> indexList = table.getIndexes();
039                System.out.println("");
040                System.out.println("There are " + Integer.toString(indexList.size()) + " indexes");
041                for (int i = 0; i < indexList.size(); i++)
042                {
043                        Index index = indexList.get(i);
044                        System.out.println(Integer.toString(i) + " " + index.getName() +
045                                " " + Boolean.toString(index.isForeignKey()) + ":" +
046                                Boolean.toString(index.isPrimaryKey()) + ":" +
047                                Boolean.toString(index.isUnique()));
048                        java.util.List<Index.ColumnDescriptor> indexColumns = index.getColumns();
049                        System.out.print("Columns:  ");
050                        for (int i2 = 0; i2 < indexColumns.size(); i2++)
051                        {
052                                Column indexItem = indexColumns.get(i).getColumn();
053                                indexedColumnName = indexItem.getName();
054                                System.out.print(indexedColumnName + "(" + Integer.toString(indexItem.getSQLType()) +")");
055                        }
056                        System.out.println(":");
057                        cursor = Cursor.createIndexCursor(table, index);
058                        // iterator = cursor.iterator();
059                        /*
060                        ** The documentation needs a mapping of SQL Types and the classes
061                        ** of the objects that are stored in the Map object.
062                        */
063                        cursor.beforeFirst();
064                        row = cursor.getNextRow();
065                        System.out.println("First row");
066                        System.out.println(row.get(indexedColumnName).getClass().getName());
067
068                        printRow(row, table);
069            cursor.afterLast();
070            System.out.println("Last row");
071            row = cursor.getPreviousRow();
072
073                        printRow(row, table);
074                }
075        }
076        protected static void printRow(java.util.Map<String,Object> row, Table table)
077        {
078                try
079                {
080                        int columnCount = table.getColumnCount();
081                        java.util.List<Column> columnList = table.getColumns();
082                        System.out.println(Integer.toString(columnCount) + " columns");
083                        for (int i = 0; i < columnCount; i ++)
084                        {
085                                String columnName = columnList.get(i).getName();
086                                short columnNumber = columnList.get(i).getColumnNumber();
087                                System.out.println(Short.toString(columnNumber) + " " + columnName + " " +
088                                        helpers.printObject(row.get(columnName)));
089                        }
090                }
091                catch (Exception e)
092                {
093                System.out.println("Exception: " + e.getClass().getName() + " " +
094                        e.getMessage());
095                e.printStackTrace();
096                }
097                
098        }
099        /**
100         * @param args
101         */
102        public static void main(String[] args) 
103        {
104                String fileName = "test.mdb";
105                if (args.length >0) { fileName = args[0]; }
106                try
107                {
108                        System.out.println("Starting thirdTest");
109                        Database db = Database.open(new java.io.File(fileName));
110            String tableNameList[] = db.getTableNames().toArray(new String[1] );
111            for (int i = 0; i < tableNameList.length; i++)
112            {
113                System.out.println("*****  *****");
114                System.out.println("*****  *****");
115                System.out.println("Processing table " + tableNameList[i]);
116                processTable(db.getTable(tableNameList[i]));
117            }
118                        db.flush();
119                        db.close();
120                        System.out.println("Ending thirdTest");
121                }
122                catch (Exception e)
123                {
124                        System.out.println("Exception " + e.getClass().getName() +
125                                        " " + e.getMessage());
126                }
127        }
128}