Example: Executing a Java Table UDF

Java Table UDF code example.

  1. Java code for a simple return_rset method. Compile into Sample.class.
    public class Sample {
    	public static void return_rset( ResultSet[] rset1 ) throws SQLException {
    		// Creates new connection back to same db.    
    		Connection conn = DriverManager.getConnection(
    			"jdbc:ianywhere:driver=Sybase IQ;UID=DBA;PWD=sql" );           
    		Statement stmt = conn.createStatement();
    		ResultSet rset = stmt.executeQuery (
    			"SELECT ID " +
    			"FROM Customers" );
    			rset1[0] = rset;
    		}
    	}
    }
    
  2. SQL statement deploying the Java class to the database:
    INSTALL JAVA NEW FROM FILE 'd:\\java\\samples\\Sample.class'
  3. SQL procedure mapping to the Java method Sample.return_rset( java.sql.ResultSet):
    CREATE PROCEDURE sample_result_set()
    RESULT ( ID int ) 
    DYNAMIC RESULT SETS 1
    EXTERNAL NAME 'Sample.return_rset([Ljava/sql/ResultSet;)V'
    LANGUAGE JAVA
    
  4. SQL procedure in a SELECT statement:
    SELECT * from sample_result_set( ) where ID < 110