Entering content frame

 Example: Displaying Column Information 

In a Unicode database, you can specify a default code for CHAR/VARCHAR columns (ASCII or UNICODE). The Java class TableDef in the example can be used to display the results of various column definitions.

TableDef Definition

import java.sql.*;

/**

 *

 */

public class TableDef

{

    private Connection connection;

    /**

     * creates a new TableDef

     */

   public

    TableDef (

        String url)

    throws SQLException, ClassNotFoundException

    {

        // load class

        Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");

        // create connection

        this.connection = DriverManager.getConnection(url,

            new java.util.Properties ());

        this.connection.setAutoCommit (false);

    }

    /**

     *

     */

    protected void

    createTable (

        String tableName,

        String createCommand)

    throws SQLException

    {

        String fullCommand = "CREATE TABLE " + tableName + " ("

            + createCommand + " )";

        Statement stmt = this.connection.createStatement ();

        stmt.execute (fullCommand);

    }

    /**

     *

     */

    protected void

    showTableDef (

        String tableName)

    throws SQLException

    {

        System.out.println ("Table: " + tableName);

        DatabaseMetaData metaData = this.connection.getMetaData ();

        ResultSet tableColumns = metaData.getColumns(null,

           metaData.getUserName (), tableName, null);

        while (tableColumns.next ()) {

            String columnName = tableColumns.getString ("COLUMN_NAME");

            String typeName = tableColumns.getString ("TYPE_NAME");

            int colSize = tableColumns.getInt ("COLUMN_SIZE");

            System.out.println ("    " + columnName

                + ": " + typeName + " (" + colSize + ")");

         }

    }

    /**

     *

     */

    protected void

    close ()

    {

        try {

            this.connection.rollback ();

            this.connection.close ();

        }

        catch (SQLException sqlExc) {

                // ignore

        }

    }

    /**

     *

     */

    static protected String

    join (

        String [] args,

        int startIndex)

    {

         if (startIndex >= args.length) {

           return null;

        }

        StringBuffer result = new StringBuffer ();

        for (int i = startIndex; i < args.length; ++i) {

            result.append(args [i]);

            result.append (' ');

        }

        return result.toString();

    }

    /**

     * used when called form the command line

     */

    public static void main (String [] args)

    throws ClassNotFoundException

    {

        String url = args [0];

        String tableName = args [1];

        String createCommand = join (args, 2);

        TableDef tableDef = null;

 

        try {

           tableDef = new TableDef (url);

            if (createCommand != null) {

                tableDef.createTable (tableName, createCommand);

            }

            tableDef.showTableDef (tableName);

        }

        catch (SQLException sqlExc) {

            System.out.println (sqlExc);

        }

        finally {

            if (tableDef != null) {

                 tableDef.close ();

            }

        }

 

    }

}

Displaying the Column Definition of a Table

java TableDef <jdbcurl> <table_name>

Creating a Temporary Table Using the Determined Column Definitions, and Displaying these Column Definitions

<command> ::= java TableDef <jdbcurl> <table_name> <column_definition>

<jdbcurl> ::= jdbc:sapdb:<database_name>?user=<user_name>&password=<password>

 

Leaving content frame