⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schema.java

📁 Town是一个100% 纯Java API
💻 JAVA
字号:
package com.workingdogs.town;

import java.io.*;
import java.sql.*;

/*
Town, a Java JDBC abstraction layer
Copyright (C) 1999  Serge Knystautas, Jon S. Stevens

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*/
/**
The Schema object represents the <a href="Column.html">Columns</a> in a
database table. It contains a collection of <a href="Column.html">Column</a> objects.

@author Jon S. Stevens <A HREF="mailto:jon@working-dogs.com">jon@working-dogs.com</A>
@author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
@version 1.0
*/
public final class Schema
{
    //private String tableName = null;
    //private String columnsAttribute = null;
    private int numberOfColumns = 0;
    private Column columns[];


    /**
     * This method was created in VisualAge.
     */
    public Schema()
    {
    }
    /**
       * Creates a Schema with all columns
       *
       * @param   conn
       * @param   tableName
       * @return     an instance of myself
       * @exception   ConnectionException
       * @exception   DataSetException
       */
    public Schema (DataSet ds,
            String tableName) throws ConnectionException, DataSetException
    {
        this (ds, tableName, "*");
    }
    /**
       * Creates a Schema with the named columns in the columnsAttribute
       *
       * @param   conn
       * @param   tableName
       * @param   columnsAttribute
       * @return     an instance of myself
       * @exception   ConnectionException
       * @exception   DataSetException
       */
    public Schema (DataSet ds, String tableName,
            String columnsAttribute) throws ConnectionException,
    DataSetException
    {
        if (columnsAttribute == null)
            columnsAttribute = "*";

        Connection conn = ds.connection ();
        Statement stmt = null;
        try
        {
            String sql = "SELECT " + columnsAttribute + " FROM " +
                    tableName + " WHERE 1 = 0";
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery (sql);
            if (rs != null)
            {
                //setTableName (tableName);
                //setAttributes (columnsAttribute);
                populate (rs.getMetaData());
            }
            else
            {
                throw new DataSetException ("Error creating schema");
            }
        }
        catch (SQLException sqle)
        {
            throw new ConnectionException (sqle);
        }
        finally { try
            {
                if (stmt != null)
                    stmt.close();
            }
            catch (SQLException sqle)
            {
                throw new ConnectionException (sqle);
            }
        } }
    /**
       * Internal method which populates this Schema object with Columns
       *
       * @param   meta
       * @exception   ConnectionException
       * @exception   DataSetException
       */
    protected Schema (ResultSetMetaData meta)
            throws ConnectionException, DataSetException
    {
        populate (meta);
    }
    /**
       * Returns the requested Column object at index i
       *
       * @param   i
       * @return     the requested column
       * @exception   DataSetException
       */
    public Column getColumn (int i) throws DataSetException
    {
        if (i == 0)
            throw new DataSetException ("Columns are 1 based");
        else if (i > numberOfColumns)
            throw new DataSetException ("There are only " +
                    numberOfColumns() + " available!");

        try
        {
            return columns[i];
        }
        catch (Exception e)
        {
            throw new DataSetException ("Column number: " +
                    numberOfColumns() + " does not exist!");
        }
    }
    /**
       * Returns the requested Column object by name
       *
       * @param   colName
       * @return     the requested column
       * @exception   DataSetException
       */
    public Column getColumn (String colName) throws DataSetException
    {
        return getColumn(index (colName));
    }
    /**
       * Returns an array of columns
       *
       * @return   an array of columns
       */
    protected Column[] getColumns()
    {
        return this.columns;
    }
    /**
       * Gets the index position of a named column
       *
       * @param   colName
       * @return     the requested column index integer
       * @exception   DataSetException
       */
    public int index (String colName) throws DataSetException
    {
        for (int i = 1; i <= numberOfColumns(); i++)
        {
            if (columns[i].name().equalsIgnoreCase (colName))
                return i;
        }
        throw new DataSetException ("Column name: " + colName + " does not exist!");
    }
    /**
       * Gets the number of columns in this Schema
       *
       * @return   integer number of columns
       */
    public int numberOfColumns()
    {
        return this.numberOfColumns;
    }
    /**
       * Internal method which populates this Schema object with Columns
       *
       * @param   meta
       * @exception   ConnectionException
       * @exception   DataSetException
       */
    protected void populate (ResultSetMetaData meta)
            throws ConnectionException, DataSetException
    {
        try
        {
            numberOfColumns = meta.getColumnCount();
            columns = new Column[numberOfColumns() + 1]; // index is 1 based
            for (int i = 1; i <= numberOfColumns(); i++)
            {
                Column col = new Column();
                col.populate (meta, i);
                columns[i] = col;
            }
        }
        catch (SQLException sqle)
        {
            throw new ConnectionException (sqle);
        }
    }
    /**
      * Produces a DTD as per the XML spec.
      * @return java.lang.String
      */
    public String toDTD ()
    {
        return "Not implemented";
    }
    /**
       * This returns a representation of this Schema
       *
       * @return     a string
       */
    public String toString()
    {
        ByteArrayOutputStream bout = new ByteArrayOutputStream ();
        PrintWriter out = new PrintWriter (bout);
        out.print ('{');
        for (int i = 1; i <= numberOfColumns; i++)
        {
            out.print ("'" + columns[i].name () + "'");
            if (i < numberOfColumns)
                out.print (',');
        }
        out.print ('}');
        out.flush ();
        return bout.toString ();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -