DataSetUtils.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:29k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataSetUtils.java,v 1.8 2005/10/10 17:00:59 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.io.Reader;
  28. import java.io.StringReader;
  29. import java.math.BigDecimal;
  30. import java.util.Date;
  31. import java.util.Set;
  32. import javax.xml.parsers.SAXParser;
  33. import javax.xml.parsers.SAXParserFactory;
  34. import org.jdesktop.dataset.provider.sql.JDBCDataConnection;
  35. import org.jdesktop.dataset.provider.sql.SQLCommand;
  36. import org.jdesktop.dataset.provider.sql.SQLDataProvider;
  37. import org.jdesktop.dataset.provider.sql.TableCommand;
  38. import org.xml.sax.Attributes;
  39. import org.xml.sax.InputSource;
  40. import org.xml.sax.helpers.DefaultHandler;
  41. /**
  42.  *
  43.  * @author rbair
  44.  */
  45. public class DataSetUtils {
  46.     
  47.     /** Creates a new instance of DataSetUtils */
  48.     private DataSetUtils() {
  49.     }
  50.     
  51.     /**
  52.      * Checks to see if the given name is valid. If not, then return false.<br>
  53.      * A valid name is one that follows the Java naming rules for
  54.      * indentifiers, <b>except</b> that Java reserved words can
  55.      * be used, and the name may begin with a number.
  56.      */
  57.     static boolean isValidName(String name) {
  58.         return !name.matches(".*[\s]");
  59.     }
  60.     
  61.     public static String getXmlSchema(DataSet ds) {
  62.         StringBuilder buffer = new StringBuilder();
  63.         buffer.append("<?xml version="1.0" standalone="yes" ?>n");
  64.         buffer.append("<xs:schema id="");
  65.         buffer.append(ds.getName());
  66.         buffer.append("" targetNamespace="http://jdesktop.org/tempuri/");
  67.         buffer.append(ds.getName());
  68.         buffer.append(".xsd" xmlns="http://javadesktop.org/tempuri/");
  69.         buffer.append(ds.getName());
  70.         buffer.append(".xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified">n");
  71.         buffer.append("t<xs:element name="");
  72.         buffer.append(ds.getName());
  73.         buffer.append("">n");
  74.         buffer.append("tt<xs:complexType>n");
  75.         buffer.append("ttt<xs:choice maxOccurs="unbounded">n");
  76.         for (DataTable table : ds.getTables()) {
  77.             if (!(table instanceof DataRelationTable)) {
  78.                 buffer.append("tttt<xs:element name="");
  79.                 buffer.append(table.getName());
  80.                 buffer.append("" appendRowSupported="");
  81.                 buffer.append(table.isAppendRowSupported());
  82.                 buffer.append("" deleteRowSupported="");
  83.                 buffer.append(table.isDeleteRowSupported());
  84.                 buffer.append("">n");
  85.                 buffer.append("ttttt<xs:complexType>n");
  86.                 buffer.append("tttttt<xs:sequence>n");
  87.                 for (DataColumn col : table.getColumns()) {
  88.                     buffer.append("ttttttt<xs:element name="");
  89.                     buffer.append(col.getName());
  90.                     buffer.append("" type="");
  91.                     if (col.getType() == String.class || col.getType() == Character.class) {
  92.                         buffer.append("xs:string");
  93.                     } else if (col.getType() == BigDecimal.class) {
  94.                         buffer.append("xs:decimal");
  95.                     } else if (col.getType() == Integer.class) {
  96.                         buffer.append("xs:integer");
  97.                     } else if (col.getType() == Boolean.class) {
  98.                         buffer.append("xs:boolean");
  99.                     } else if (col.getType() == Date.class) {
  100.                         buffer.append("xs:dateTime");
  101.                     } else if (col.getType() == Byte.class) {
  102.                         buffer.append("xs:unsignedByte");
  103.                     } else {
  104.                         System.out.println("Couldn't find type for xsd for Class " + col.getType());
  105.                     }
  106.                     if (col.getDefaultValue() != null) {
  107.                         buffer.append("" default="");
  108.                         buffer.append(col.getDefaultValue());
  109.                     }
  110.                     if (!col.isRequired()) {
  111.                         buffer.append("" minOccurs="0");
  112.                     }
  113.                     buffer.append("" keyColumn="");
  114.                     buffer.append(col.isKeyColumn());
  115.                     buffer.append("" readOnly="");
  116.                     buffer.append(col.isReadOnly());
  117.                     if (col.getExpression() != null && !(col.getExpression().trim().equals(""))) {
  118.                         buffer.append("" expression="");
  119.                         buffer.append(col.getExpression());
  120.                     }
  121.                     buffer.append("" />n");
  122.                 }
  123.                 buffer.append("tttttt</xs:sequence>n");
  124.                 buffer.append("ttttt</xs:complexType>n");
  125.                 buffer.append("tttt</xs:element>n");
  126.             }
  127.         }
  128.         buffer.append("ttt</xs:choice>n");
  129.         buffer.append("tt</xs:complexType>n");
  130.         buffer.append("tt<xs:annotation>n");
  131.         buffer.append("ttt<xs:appinfo>n");
  132.         
  133.         //write the relations out
  134.         for (DataRelation r : ds.getRelations()) {
  135.             buffer.append("tttt<dataRelation name="");
  136.             buffer.append(r.getName());
  137.             buffer.append("" parentColumn="");
  138.             DataColumn col = r.getParentColumn();
  139.             if (col != null) {
  140.                 buffer.append(col.getTable().getName());
  141.                 buffer.append(".");
  142.                 buffer.append(col.getName());
  143.             }
  144.             buffer.append("" childColumn="");
  145.             col = r.getChildColumn();
  146.             if (col != null) {
  147.                 buffer.append(col.getTable().getName());
  148.                 buffer.append(".");
  149.                 buffer.append(col.getName());
  150.             }
  151.             buffer.append("" />n");
  152.         }
  153.         //write the data relation tables out
  154.         for (DataTable table : ds.getTables()) {
  155.             if (table instanceof DataRelationTable) {
  156.                 DataRelationTable drt = (DataRelationTable)table;
  157.                 buffer.append("tttt<dataRelationTable name="");
  158.                 buffer.append(drt.getName());
  159.                 buffer.append("" relation="");
  160.                 DataRelation dr = drt.getRelation();
  161.                 buffer.append(dr == null ? "" : dr.getName());
  162.                 buffer.append("" parentSelector="");
  163.                 DataSelector sel = drt.getParentSelector();
  164.                 buffer.append(sel == null ? "" : sel.getName());
  165.                 buffer.append("" parentTable="");
  166.                 DataTable parent = drt.getParentTable();
  167.                 buffer.append(parent == null ? "" : parent.getName());
  168.                 buffer.append("" />n");
  169.             }
  170.         }
  171.         
  172.         //write the data values out
  173.         for (DataValue value : ds.getValues()) {
  174.             buffer.append("tttt<dataValue name="");
  175.             buffer.append(value.getName());
  176.             buffer.append("" expression="");
  177.             if (value.getExpression() != null) {
  178.                 buffer.append(value.getExpression());
  179.             }
  180.             buffer.append("" />n");
  181.         }
  182.         
  183.         //close the annotation section
  184.         buffer.append("ttt</xs:appinfo>n");
  185.         buffer.append("tt</xs:annotation>n");
  186.         //close the document down
  187.         buffer.append("t</xs:element>n");
  188.         buffer.append("</xs:schema>n");
  189.         return buffer.toString();
  190.     }
  191.     
  192.     public static DataSet createFromXmlSchema(String schema) {
  193.         return createFromXmlSchema(new StringReader(schema));
  194.     }
  195.     
  196.     public static DataSet createFromXmlSchema(File f) throws FileNotFoundException {
  197.         return createFromXmlSchema(new FileInputStream(f));
  198.     }
  199.     
  200.     public static DataSet createFromXmlSchema(InputStream is) {
  201.         return createFromXmlSchema(new InputStreamReader(is));
  202.     }
  203.     
  204.     public static DataSet createFromXmlSchema(Reader schema) {
  205.         DataSet ds = new DataSet();
  206.         //set up an XML parser to parse the schema
  207.         try {
  208.             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  209.             InputSource is = new InputSource(schema);
  210.             parser.parse(is, new DataSetParser(ds));
  211.         } catch (Exception e) {
  212.             e.printStackTrace();
  213.         }
  214.         return ds;
  215.     }
  216.     
  217.     /**
  218.      * parses the document.
  219.      *
  220.      * The xs:element tag is used repeatedly. With a depth of 0, it is the
  221.      * DataSet element. With a depth of 1, it is a DataTable, and with a depth of
  222.      * 2 it is a DataColumn
  223.      */
  224.     private static final class DataSetParser extends DefaultHandler {
  225.         public int elementDepth = 0;
  226.         private Attributes attrs;
  227.         private DataSet ds;
  228.         private DataTable table;
  229.         private DataColumn column;
  230.         private DataProvider dataProvider;
  231.         
  232.         public DataSetParser(DataSet ds) {
  233.             this.ds = ds == null ? new DataSet() : ds;
  234.         }
  235.         
  236.         public DataSet getDataSet() {
  237.             return ds;
  238.         }
  239.         
  240.         public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException {
  241.             this.attrs = atts;
  242.             if (qName.equals("xs:element")) {
  243.                 elementDepth++;
  244.                 switch(elementDepth) {
  245.                     case 1:
  246.                         //this is a DataSet
  247.                         ds.setName(attrs.getValue("name"));
  248.                         break;
  249.                     case 2:
  250.                         //this is a table tag
  251.                         table = ds.createTable(attrs.getValue("name"));
  252.                         String val = attrs.getValue("appendRowSupported");
  253.                         table.setAppendRowSupported(val == null || val.equalsIgnoreCase("true"));
  254.                         val = attrs.getValue("deleteRowSupported");
  255.                         table.setDeleteRowSupported(val == null || val.equalsIgnoreCase("true"));
  256.                         break;
  257.                     case 3:
  258.                         //this is a column tag
  259.                         column = table.createColumn(attrs.getValue("name"));
  260.                         //set the required flag
  261.                         val = attrs.getValue("minOccurs");
  262.                         if (val != null && val.equals("")) {
  263.                             column.setRequired(true);
  264.                         }
  265.                         //find out if this is a keycolumn
  266.                         val = attrs.getValue("keyColumn");
  267.                         column.setKeyColumn(val == null ? false : val.equalsIgnoreCase("true"));
  268.                         //find out if this column is readOnly
  269.                         val = attrs.getValue("readOnly");
  270.                         column.setReadOnly(val == null ? false : val.equalsIgnoreCase("true"));
  271.                         //grab the default, if one is supplied
  272.                         String defaultValue = attrs.getValue("default"); //TODO This will require some kind of type conversion
  273.                         //get the type. Convert from XSD types to java types
  274.                         val = attrs.getValue("type");
  275.                         if (val.equals("xs:string")) {
  276.                             column.setType(String.class);
  277.                             if (defaultValue != null && !defaultValue.equals("")) {
  278.                                 column.setDefaultValue(defaultValue);
  279.                             }
  280.                         } else if (val.equals("xs:decimal")) {
  281.                             column.setType(BigDecimal.class);
  282.                             if (defaultValue != null && !defaultValue.equals("")) {
  283.                                 column.setDefaultValue(new BigDecimal(defaultValue));
  284.                             }
  285.                         } else if (val.equals("xs:integer") || val.equals("xs:int")) {
  286.                             column.setType(Integer.class);
  287.                             if (defaultValue != null && !defaultValue.equals("")) {
  288.                                 column.setDefaultValue(new Integer(defaultValue));
  289.                             }
  290.                         } else if (val.equals("xs:boolean")) {
  291.                             column.setType(Boolean.class);
  292.                             if (defaultValue != null && !defaultValue.equals("")) {
  293.                                 column.setDefaultValue(Boolean.parseBoolean(defaultValue));
  294.                             }
  295.                         } else if (val.equals("xs:date") || val.equals("xs:time") || val.equals("xs.dateTime")) {
  296.                             column.setType(Date.class);
  297.                             if (defaultValue != null && !defaultValue.equals("")) {
  298.                                 column.setDefaultValue(new Date(Date.parse(defaultValue)));
  299.                             }
  300.                         } else if (val.equals("xs:unsignedByte")) {
  301.                             column.setType(Byte.class);
  302.                             if (defaultValue != null && !defaultValue.equals("")) {
  303.                                 column.setDefaultValue(new Byte(defaultValue));
  304.                             }
  305.                         } else {
  306.                             System.err.println("unexpected classType: '"  + val + "'");
  307.                         }
  308.                         //set the column expression
  309.                         val = attrs.getValue("expression");
  310.                         if (val != null && !("".equals(val))) {
  311.                             column.setExpression(val);
  312.                         }
  313.                         break;
  314.                     default:
  315.                         //error condition
  316.                         System.out.println("Error in DataSetParser");
  317.                 }
  318.             } else if (qName.equals("dataProvider")) {
  319.                 String classType = attrs.getValue("class");
  320.                 if (classType != null) {
  321.                     try {
  322.                         dataProvider = (DataProvider)Class.forName(classType).newInstance();
  323.                         table.setDataProvider(dataProvider);
  324.                         //TODO There needs to be a more general configuration solution
  325.                         if (dataProvider instanceof SQLDataProvider) {
  326.                             String tableName = attrs.getValue("tableName");
  327.                             if (tableName != null && !tableName.equals("")) {
  328.                                 TableCommand cmd = new TableCommand(tableName);
  329.                                 cmd.setWhereClause(attrs.getValue("whereClause"));
  330.                                 cmd.setOrderByClause(attrs.getValue("orderByClause"));
  331.                                 cmd.setHavingClause(attrs.getValue("havingClause"));
  332.                                 dataProvider.setCommand(cmd);
  333.                             } else {
  334.                                 SQLCommand command = new SQLCommand();
  335.                                 command.setCustom(true);
  336.                                 command.setSelectSQL(attrs.getValue("select"));
  337.                                 command.setInsertSQL(attrs.getValue("insert"));
  338.                                 command.setUpdateSQL(attrs.getValue("update"));
  339.                                 command.setDeleteSQL(attrs.getValue("delete"));
  340.                                 dataProvider.setCommand(command);
  341.                             }
  342.                         }
  343.                     } catch (Exception e) {
  344.                         //hmmm
  345.                         e.printStackTrace();
  346.                     }
  347.                 }
  348.             } else if (qName.equals("dataRelationTable")) {
  349.                 DataRelationTable drt = ds.createRelationTable(attrs.getValue("name"));
  350.                 drt.setRelation((DataRelation)ds.getElement(attrs.getValue("relation")));
  351.                 drt.setParentSelector((DataSelector)ds.getElement(attrs.getValue("parentSelector")));
  352.                 drt.setParentTable((DataTable)ds.getElement(attrs.getValue("parentTable")));
  353.             } else if (qName.equals("dataRelation")) {
  354.                 DataRelation relation = ds.createRelation(attrs.getValue("name"));
  355.                 relation.setParentColumn((DataColumn)ds.getElement(attrs.getValue("parentColumn")));
  356.                 relation.setChildColumn((DataColumn)ds.getElement(attrs.getValue("childColumn")));
  357.             } else if (qName.equals("dataValue")) {
  358.                 DataValue value = ds.createValue(attrs.getValue("name"));
  359.                 value.setExpression(attrs.getValue("expression"));
  360.             }
  361.         }
  362.         public void endElement(String uri, String localName, String qName) throws org.xml.sax.SAXException {
  363.             if (qName.equals("xs:element")) {
  364.                 switch(elementDepth) {
  365.                     case 1:
  366.                         //this is a dataset tag
  367.                         break;
  368.                     case 2:
  369.                         //this is a table tag
  370.                         break;
  371.                     case 3:
  372.                         //this is a column tag
  373.                         break;
  374.                     default:
  375.                         //error condition
  376.                         System.out.println("Error in DataSetParser");
  377.                 }
  378.                 elementDepth--;
  379.             }
  380.         }
  381.         public void endDocument() throws org.xml.sax.SAXException {
  382.         }
  383.     }
  384.     public static DataSet createFromDatabaseSchema(JDBCDataConnection conn, String databaseName, String... names) {
  385.         java.util.Set<String> set = new java.util.HashSet<String>();
  386.         for (String name : names) {
  387.             set.add(name);
  388.         }
  389.         DataSet ds = createFromDatabaseSchema(conn.getConnection(), databaseName, set);
  390.         for (DataTable table : ds.getTables()) {
  391.             if (!(table instanceof DataRelationTable) && table.getDataProvider() != null) {
  392.                 table.getDataProvider().setConnection(conn);
  393.             }
  394.         }
  395.         return ds;
  396.     }
  397.     
  398.     /**
  399.      * Creates a DataSet based on some DatabaseMetaData information. This method
  400.      * takes a Set of database entity &quot;names&quot; which are of the form:<br>
  401.      * table or table.column
  402.      */
  403.     public static DataSet createFromDatabaseSchema(java.sql.Connection conn, String databaseName, java.util.Set<String> names) {
  404.         DataSet ds = new DataSet();
  405.         try {
  406.             java.util.Set<String> tableNames = new java.util.HashSet<String>();
  407.             for (String name : names) {
  408.                 if (name.contains(".")) {
  409.                     tableNames.add(name.substring(0, name.indexOf(".")));
  410.                 } else {
  411.                     tableNames.add(name);
  412.                 }
  413.             }
  414.             java.sql.DatabaseMetaData md = conn.getMetaData();
  415. //            printResultSet(md.getCatalogs());
  416. //            printResultSet(md.getSchemas());
  417. //            printResultSet(md.getTables(databaseName, null, "package", null));
  418.             for (String tableName : tableNames) {
  419.                 DataTable table = ds.createTable(tableName);
  420.                 //check to see whether the entire table is to be included, or only some of the table
  421.                 if (names.contains(tableName)) {
  422.                     //import all of the table columns
  423.                     System.out.println("Including all columns");
  424.                     java.sql.ResultSet rs = md.getColumns(databaseName, null, tableName, null);
  425.                     while (rs.next()) {
  426.                         DataColumn col = table.createColumn(rs.getString(4));
  427.                         col.setDefaultValue(rs.getObject(13));
  428. //                        col.setKeyColumn(value);
  429.                         col.setReadOnly(false);
  430.                         col.setRequired(rs.getString(18).equals("NO"));
  431.                         col.setType(getType(rs.getInt(5)));
  432.                     }
  433.                 } else {
  434.                     //import the key columns, and the specified columns
  435.                     //first, create a set of columns for this table, including
  436.                     //the key column names
  437.                     java.util.Set<String> colNames = new java.util.HashSet<String>();
  438.                     for (String name : names) {
  439.                         if (name.startsWith(tableName) && name.contains(".")) {
  440.                             String colName = name.substring(name.indexOf(".") +1);
  441.                             colNames.add(colName);
  442.                         }
  443.                     }
  444.                     java.sql.ResultSet rs = md.getPrimaryKeys(databaseName, null, tableName);
  445.                     while (rs.next()) {
  446.                         colNames.add(rs.getString(4));
  447.                     }
  448.                 
  449.                     //now, get the column information for these columns
  450.                     System.out.println("Including columns: " + colNames);
  451.                     for (String colName : colNames) {
  452.                         rs = md.getColumns(databaseName, null, tableName, colName);
  453.                         while (rs.next()) {
  454.                             DataColumn col = table.createColumn(rs.getString(4));
  455.                             col.setDefaultValue(rs.getObject(13));
  456. //                            col.setKeyColumn(value);
  457.                             col.setReadOnly(false);
  458.                             col.setRequired(rs.getString(18).equals("NO"));
  459.                             col.setType(getType(rs.getInt(5)));
  460.                         }
  461.                     }
  462.                     
  463. //                md.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
  464. //                printResultSet(md.getExportedKeys(databaseName, null, tableName));
  465. //                printResultSet(md.getImportedKeys(databaseName, null, tableName));
  466. //                md.getTables(catalog, schemaPattern, tableNamePattern, types);
  467.                 }
  468.                 
  469.                 //create the DataProvider
  470.                 org.jdesktop.dataset.provider.sql.SQLDataProvider dp = new org.jdesktop.dataset.provider.sql.SQLDataProvider(tableName);
  471.                 table.setDataProvider(dp);
  472.             }
  473.             
  474.             //now that all of the tables are created, create the relations
  475.             for (String tableName : tableNames) {
  476.                 java.sql.ResultSet rs = md.getImportedKeys(databaseName, null, tableName);
  477. //                System.out.println(tableName + ":");
  478. //                printResultSet(rs);
  479.                 while (rs.next()) {
  480.                     //TODO for now, DataRelations only support a single column,
  481.                     //so if the sequence is > 1, skip it
  482.                     if (rs.getInt(9) > 1) {
  483.                         continue;
  484.                     }
  485.                     String childTableName = rs.getString(7);
  486.                     String childColName = rs.getString(8);
  487.                     DataTable parentTable = ds.getTable(tableName);
  488.                     DataColumn parentColumn = parentTable.getColumn(rs.getString(4));
  489.                     DataTable childTable = ds.getTable(childTableName);
  490.                     DataColumn childColumn = childTable.getColumn(childColName);
  491.                     if (parentColumn != null && childColumn != null && parentColumn != childColumn) {
  492.                         DataRelation rel = ds.createRelation(rs.getString(12));
  493.                         rel.setParentColumn(parentColumn);
  494.                         rel.setChildColumn(childColumn);
  495.                     } else if (parentColumn == childColumn) {
  496.                         System.out.println("column identity: " + childTableName + "." + childColName + " = " + parentTable.getName() + "." + parentColumn.getName());
  497.                     }
  498.                 }
  499.             }
  500.         } catch (Exception e) {
  501.             e.printStackTrace();
  502.         }
  503.         
  504.         return ds;
  505.     }
  506.     
  507.     private static Class getType(int type) {
  508.         switch (type) {
  509.             case java.sql.Types.ARRAY:
  510.                 return Object.class;
  511.             case java.sql.Types.BIGINT:
  512.                 return java.math.BigInteger.class;
  513.             case java.sql.Types.BINARY:
  514.                 return Boolean.class;
  515.             case java.sql.Types.BIT:
  516.                 return Boolean.class;
  517.             case java.sql.Types.BLOB:
  518.                 return byte[].class;
  519.             case java.sql.Types.BOOLEAN:
  520.                 return Boolean.class;
  521.             case java.sql.Types.CHAR:
  522.                 return Character.class;
  523.             case java.sql.Types.CLOB:
  524.                 return char[].class;
  525.             case java.sql.Types.DATALINK:
  526.                 return Object.class;
  527.             case java.sql.Types.DATE:
  528.                 return java.util.Date.class;
  529.             case java.sql.Types.DECIMAL:
  530.                 return java.math.BigDecimal.class;
  531.             case java.sql.Types.DISTINCT:
  532.                 return Object.class;
  533.             case java.sql.Types.DOUBLE:
  534.                 return BigDecimal.class;
  535.             case java.sql.Types.FLOAT:
  536.                 return java.math.BigDecimal.class;
  537.             case java.sql.Types.INTEGER:
  538.                 return java.math.BigInteger.class;
  539.             case java.sql.Types.JAVA_OBJECT:
  540.                 return Object.class;
  541.             case java.sql.Types.LONGVARBINARY:
  542.                 return byte[].class;
  543.             case java.sql.Types.LONGVARCHAR:
  544.                 return String.class;
  545.             case java.sql.Types.NULL:
  546.                 return Object.class;
  547.             case java.sql.Types.NUMERIC:
  548.                 return java.math.BigDecimal.class;
  549.             case java.sql.Types.OTHER:
  550.                 return Object.class;
  551.             case java.sql.Types.REAL:
  552.                 return java.math.BigDecimal.class;
  553.             case java.sql.Types.REF:
  554.                 return Object.class;
  555.             case java.sql.Types.SMALLINT:
  556.                 return Integer.class;
  557.             case java.sql.Types.STRUCT:
  558.                 return Object.class;
  559.             case java.sql.Types.TIME:
  560.                 return java.util.Date.class;
  561.             case java.sql.Types.TIMESTAMP:
  562.                 return java.util.Date.class;
  563.             case java.sql.Types.TINYINT:
  564.                 return Integer.class;
  565.             case java.sql.Types.VARBINARY:
  566.                 return byte[].class;
  567.             case java.sql.Types.VARCHAR:
  568.                 return String.class;
  569.             default:
  570.                 System.out.println("Unsupported type");
  571.                 return Object.class;
  572.         }
  573.     }
  574.     
  575.     //helper method used for debugging
  576.     private static void printResultSet(java.sql.ResultSet rs) throws Exception {
  577.         //print out the result set md
  578.         java.sql.ResultSetMetaData md = rs.getMetaData();
  579.         StringBuilder buffer = new StringBuilder();
  580.         StringBuilder lineBuffer = new StringBuilder();
  581.         for (int i=0; i<md.getColumnCount(); i++) {
  582.             buffer.append(pad(md.getColumnName(i+1), ' '));
  583.             for (int j=0; j<20; j++) {
  584.                 lineBuffer.append("-");
  585.             }
  586.             if (i < (md.getColumnCount() -1)) {
  587.                 buffer.append(" | ");
  588.                 lineBuffer.append("---");
  589.             }
  590.         }
  591.         System.out.println(buffer.toString());
  592.         System.out.println(lineBuffer.toString());
  593.         
  594.         buffer = new StringBuilder();
  595.         while (rs.next()) {
  596.             for (int i=0; i<md.getColumnCount(); i++) {
  597.                 Object obj = rs.getObject(i+1);
  598.                 String data = obj == null ? "<null>" : obj.toString();
  599.                 data = pad(data, ' ');
  600.                 buffer.append(data);
  601.                 if (i < (md.getColumnCount() -1)) {
  602.                     buffer.append(" | ");
  603.                 }
  604.             }
  605.             buffer.append("n");
  606.         }
  607.         System.out.println(buffer.toString());
  608.     }
  609.     
  610.     //helper method used for debugging
  611.     private static String pad(String s, char padChar) {
  612.         if (s == null) {
  613.             return "<null>              ";
  614.         } else if (s.length() > 20) {
  615.             return s.substring(0, 20);
  616.         } else if (s.length() < 20) {
  617.             StringBuilder buffer = new StringBuilder(s);
  618.             for (int i=s.length(); i<20; i++) {
  619.                 buffer.append(padChar);
  620.             }
  621.             return buffer.toString();
  622.         } else {
  623.             return s;
  624.         }
  625.     }
  626. }