Java classes

This section shows the simple Java classes that this chapter uses to illustrate Java in Adaptive Server. You can also find these classes and their Java source code in $SYBASE/$SYBASE_ASE/sample/JavaSql. (UNIX) or %SYBASE%\Ase-12_5\sample\JavaSql (Windows NT).

This is the Address class:

//
// Copyright (c) 1999
// Sybase, Inc
// Emeryville, CA 94608
// All Rights Reserved
//
/**
* A simple class for address data, to illustrate using a Java class 
* as a SQL datatype.
*/
public class  Address implements java.io.Serializable  {

/**
* The street data for the address.
* @serial A simple String value.
*/
 	public String street;

/** 
* The zipcode data for the address.
* @serial A simple String value.
*/
 	String zip;

/** A default constructor.
*/
	public Address ( ) {
 		street = "Unknown";
 		zip = "None";
 	}
/**
* A constructor with parameters
* @param S 				a string with the street information
* @param Z 				a string with the zipcode information
*/
 	public Address (String S, String Z) {
 		street = S;
 		zip = Z;
 	}
/** 
* A method to return a display of the address data.
* @returns a string with a display version of the address data.
*/
 	public String toString( ) {
 		return  "Street= " + street +  "  ZIP= " + zip;
 	}
/** 
* A void method to remove leading blanks.
* This method uses the static method
* <code>Misc.stripLeadingBlanks</code>.
*/
	public void removeLeadingBlanks( ) {
 	street = Misc.stripLeadingBlanks(street);
 	zip = Misc.stripLeadingBlanks(street);
 	}
}

This is the Address2Line class, which is a subclass of the Address class:

//
// Copyright (c) 1999
// Sybase, Inc
// Emeryville, CA 94608
// All Rights Reserved
//
/**
* A subclass of the Address class that adds a seond line of address data,
* <p>This is a simple subclass to illustrate using a Java subclass
* as a SQL datatype.
*/
public class Address2Line extends Address implements java.io.Serializable  {

/**
* The second line of street data for the address.
* @serial a simple String value
*/
 	String line2;
/**
* A default constructor
*/
 	public Address2Line ( ) {
 		street = "Unknown";
 		line2 = " ";
 		zip = "None";
 	}
/**
* A constructor with parameters.
* @param S	 a string with the street information
* @param L2 a string with the second line of address data
* @param Z a string with the zipcode information
*/
public Address2Line (String S, String L2, String Z) {
 	street = S;
 	line2 = L2;
 	zip = Z;
}

/**
* A method to return a display of the address data
* @returns a string with a display version of the address data
*/

public String toString( ) {
 	return "Street= " + street + " Line2= " + line2 +  " ZIP= " + zip;
}

/**
* A void method to remove leading blanks.
* This method uses the static method
* <code>Misc.stripLeadingBlanks</code>.
*/

	public void removeLeadingBlanks( ) {
 		line2  = Misc.stripLeadingBlanks(line2);
 		super.removeLeadingBlanks( );
 	}
}

The Misc class contains sets of miscellaneous routines:

//
// Copyright (c) 1999
// Sybase, Inc
// Emeryville, CA 94608
// All Rights Reserved
//
/**
* A non-instantiable class with miscellaneous static methods
* that illustrate the use of Java methods in SQL.
*/

public class Misc{

/**
* The Misc class contains only static methods and cannot be instantiated.
*/

private Misc( ) { }

/**
* Removes leading blanks from a String
*/
	public static String stripLeadingBlanks(String s) {
 		if (s == null) return null;
 		for (int scan=0; scan<s.length( ); scan++)
 			if (!java.lang.Character.isWhitespace(s.charAt(scan) )) 
 				break;
 			} else if (scan == s.length( )){
				return  "";
 			} else return s.substring(scan);
			}
 		}
	}
	return  "";
}
/**
* Extracts the street number from an address line.
* e.g., Misc.getNumber("   123 Main Street") == 123
*	 Misc.getNumber("   Main Street")  == 0
*	 Misc.getNumber("") == 0
*	 Misc.getNumber("   123   ") ==  123
*	 Misc.getNumber("   Main 123 ")  == 0 
* @param s a string assumed to have address data
* @return a string with the extracted street number
*/

	public static int getNumber (String s) {
 		String stripped = stripLeadingBlanks(s);
		if (s==null) return -1;
 		for(int right=0; right < stripped.length( ); right++){
 			if (!java.lang.Character.isDigit(stripped.charAt(right))) {
				break;
 			} else if (right==0){
				return 0;
 			} else {
				return java.lang.Integer.parseInt
					(stripped.substring(0, right), 10);
 			}
			}
			return -1;
		}

/**
* Extract the "street" from an address line.
* e.g.,  Misc.getStreet("   123 Main Street") ==  "Main Street"
*	Misc.getStreet("   Main Street")  ==   "Main Street"
*	Misc.getStreet("") ==  ""
*	Misc.getStreet("   123   ") ==  ""
*	Misc.getStreet("   Main 123 ")  == "Main 123"
* @param s a string assumed to have address data
* @return a string with the extracted street name
*/
	public static String getStreet(String s) {
 		int left;
		if (s==null) return null;
 		for (left=0; left<s.length( ); left++){
			if(java.lang.Character.isLetter(s.charAt(left))) {
				break;
			} else if (left == s.length( )) {
				return "";
 			} else {
				return s.substring(left);
			}
		}
		return "";
 	}
}