SQLJ example classes for Sybase ASE 12.5


Class SQLJExamples

java.lang.Object
  |
  +--SQLJExamples

public class SQLJExamples
extends java.lang.Object

A class with methods to illustrate SQLJ facilities.


Constructor Summary
SQLJExamples()
           
 
Method Summary
static void bestTwoEmps(java.lang.String[] n1, java.lang.String[] id1, java.math.BigDecimal[] s1, int[] r1, java.lang.String[] n2, java.lang.String[] id2, java.math.BigDecimal[] s2, int[] r2, int regionParm)
          A method that provides the column data for the top two employees whose region is above a given value.
static void correctStates(java.lang.String oldSpelling, java.lang.String newSpelling)
          A method that updates the "salesemp" table to correct the spelling of a given state.
static java.lang.String job(int jc)
          A method that returns the job name for a given job code.
static void orderedEmps(int regionParm, java.sql.ResultSet[] rs)
          An example method that returns a result set.
static int region(java.lang.String s)
          A method that returns a code for the geographic region of a given state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLJExamples

public SQLJExamples()
Method Detail

region

public static int region(java.lang.String s)
                  throws java.sql.SQLException
A method that returns a code for the geographic region of a given state.

This method is intended for use with an SQLJ create function such as the following:

  create function regionof(state char(20)) 
		returns integer
  language java parameter style java
  external name 'Routines1.region(java.lang.String)'
  

That SQLJ function "regionof" would be called in SQL as follows:

  select name, regionof(state) as region
  	from salesemps
  where regionof(state)=3
  
Parameters:
s - the given state abbreviation

correctStates

public static void correctStates(java.lang.String oldSpelling,
                                 java.lang.String newSpelling)
                          throws java.sql.SQLException
A method that updates the "salesemp" table to correct the spelling of a given state.

This method is intended for use with an SQLJ create procedure such as the following:

  create procedure correctstates(old char(20), 
		notold char(20))
	modifies sql data
	language java parameter style java
	external name
		'Routines1.correctStates(java.lang.String
		java.lang.String)'

  

That SQLJ procedure 'correctstates' would be called in SQL as follows:

  execute correctstates 'GEO', 'GA'
  
Parameters:
oldSpelling - the incorrect spelling, e.g. "Calif"
newSpelling - the correct spelling, e.g. "CA"

bestTwoEmps

public static void bestTwoEmps(java.lang.String[] n1,
                               java.lang.String[] id1,
                               java.math.BigDecimal[] s1,
                               int[] r1,
                               java.lang.String[] n2,
                               java.lang.String[] id2,
                               java.math.BigDecimal[] s2,
                               int[] r2,
                               int regionParm)
                        throws java.sql.SQLException
A method that provides the column data for the top two employees whose region is above a given value.

The method is intended for an SQLJ create procedure such as the following:

  create procedure best2
  	(out n1 varchar(50), out id1 varchar(5), out s1 decimal(6,2), out r1 integer, 
  	out n2 varchar(50), out id2 varchar(50), out r2 integer, out s2 decimal(6,2), 
  	in region integer)
  	language java parameter style java
  	external name 'Routines2.bestTwoEmps(java.lang.String[],
  		java.lang.String[], int[],
  		java.math.BigDecimal[], java.lang.String[],
  		java.lang.String[], int[],
  		java.math.BigDecimal[], int)'
  

That SQLJ procedure "best2" would be called in SQL as follows:

  declare @n1 varchar(50), @id1 varchar(5),
Parameters:
n1 - a string array for the first output name
id1 - a string array for the first output id
r1 - a string array for the first output region
s1 - a string array for the first output sales
n2 - a string array for the second output name
id2 - a string array for the second output id
r2 - a string array for the second output region
s2 - a string array for the second output sales
regionParm - an int for the given region

orderedEmps

public static void orderedEmps(int regionParm,
                               java.sql.ResultSet[] rs)
                        throws java.sql.SQLException
An example method that returns a result set.

This method is intended for an SQLJ create procedure such as the following:

  create procedure rankedemps(region integer)
  	dynamic result sets 1
  	language java parameter style java
  	external name 'Routines3.orderedEmps(int)'  
  

That SQLJ procedure "rankedemps" would be called as a stored procedure by an SQL client. For example, a Java client such as the following:

  java.sql.CallableStatement stmt =
  	conn.prepareCall({call rankedemps(?)});
  stmt.setInt(1,3);
  ResultSet rs = stmt.executeQuery();
  while (rs.next()) {
  	String name = rs.getString(1);
  	int.region = rs.getInt(2);
  	BigDecimal sales = rs.get.BigDecimal(3);
  	System.out.print(Name =  + name);
  	System.out.print(Region =  + region);
  	System.out.print(Sales =  + sales);
  	System.out.printIn():
  }
  

The main method of the Java class CallRankedEmps contains such a call of "rankedemps".

Parameters:
int - an int with a region value
rs - the result set that will be returned on the SQLJ call

job

public static java.lang.String job(int jc)
                            throws java.sql.SQLException
A method that returns the job name for a given job code.

This method is intended to illustrate the handling of nulls with the SQLJ null on null input clause. You would define an SQLJ function on this method, as follows:

  create function jobof22(jc integer) returns varchar(20)
  returns null on null input
  language java parameter style java
  external name 'Routines5.job2(int)'    
  

A call of the SQLJ "job22" function will implicitly return null (without calling "Routines5.job2" if the parameter value is null.

Parameters:
jc - an int with the given job code

SQLJ example classes for Sybase ASE 12.5