Description: The following simple table function selects rows from a foreign database.
Keywords: Functions, table function example

# Example Derby-style table function

The following simple table function selects rows from a foreign database.

    package com.acme.hrSchema;

    import java.sql.*;

    /**
     * Sample Table Function for reading the employee table in an
     * external database.
     */
    public class EmployeeTable
    {
        public  static  ResultSet   read()
            throws SQLException
        {
            Connection          conn = getConnection();
            PreparedStatement   ps = conn.prepareStatement( "select * from hrSchema.EmployeeTable" );

            return ps.executeQuery();
        }

        protected  static  Connection    getConnection()
            throws SQLException
        {
            String  EXTERNAL_DRIVER = "com.mysql.jdbc.Driver";

            try { Class.forName( EXTERNAL_DRIVER ); }
            catch (ClassNotFoundException e) { throw new SQLException( "Could not find class " + EXTERNAL_DRIVER ); }

            Connection          conn = DriverManager.getConnection
                ( "jdbc:mysql://localhost/hr?user=root&password=mysql-passwd" );

            return conn;
        }
    }

Parent topic: Programming Derby-style table functions