home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 December
/
PCWorld_1998-12_cd.iso
/
software
/
sybase
/
ASA
/
asa60.exe
/
data1.cab
/
jxmp_files
/
JDBCExamples.java
< prev
next >
Wrap
Text File
|
1998-07-27
|
9KB
|
298 lines
// Import the necessary classes
import java.sql.*; // JDBC
import com.sybase.jdbc.*; // Sybase jConnect
import java.util.Properties; // Properties
import sybase.sql.*; // Sybase utilities
import asademo.*; // Example classes
public class JDBCExamples{
private static Connection conn;
public static void main( String args[] ){
conn = null;
String machineName;
if ( args.length != 1 ) {
machineName = "localhost";
} else {
machineName = new String( args[0] );
}
ASAConnect( "dba", "sql", machineName );
if( conn!=null ) {
System.out.println( "Connection successful" );
}else{
System.out.println( "Connection failed" );
}
try{
serializeVariable();
serializeColumn();
serializeColumnCastClass();
}
catch( Exception e ) {
System.out.println( "Error: " + e.getMessage() );
e.printStackTrace();
}
}
// Create a native Java object in database
// server and pass object to client.
private static void serializeVariable() throws Exception {
Statement stmt;
ResultSet rs;
byte arrayb[];
String strTestObject;
if ( !conn.isClosed() ) {
stmt = conn.createStatement();
stmt.execute( "CREATE VARIABLE strServerSide java.lang.String;" );
stmt.execute( "SET strServerSide = new java.lang.String(\'Java string created on server\');" );
stmt.execute( "CREATE VARIABLE long_bin long binary" );
stmt.execute( "SET long_bin = sybase.sql.ASAUtils.toByteArray( strServerSide )" );
rs = stmt.executeQuery( "SELECT long_bin" );
rs.next(); // move to first item in resultset
arrayb = rs.getBytes(1); // assign contents of long_bin to byte array
// The type of object on the server
// must be known to cast it correctly
strTestObject = (java.lang.String)sybase.sql.ASAUtils.fromByteArray( arrayb );
System.out.println( "Java String object created on ASA server, contains: " +
strTestObject.toString() + "\n\n" );
}
}
// Return a result set from a column containing Java objects
private static void serializeColumn() throws Exception {
Statement stmt;
ResultSet rs;
byte arrayb[];
asademo.ContactInfo ci;
String name;
if ( conn != null ) {
stmt = conn.createStatement();
rs = stmt.executeQuery( "SELECT "
+ "sybase.sql.ASAUtils.toByteArray( JName.getName() ) AS Name, "
+ "sybase.sql.ASAUtils.toByteArray( jdba.contact.JContactInfo ) "
+ "FROM jdba.contact" );
while ( rs.next() ) {
arrayb = rs.getBytes("Name");
name = ( String )sybase.sql.ASAUtils.fromByteArray( arrayb );
arrayb = rs.getBytes(2);
ci = (asademo.ContactInfo)sybase.sql.ASAUtils.fromByteArray( arrayb );
System.out.println( "Name: " + name +
"\n\tStreet: " + ci.street +
"\n\tCity: " + ci.city +
"\n\tState: " + ci.state +
"\n\tPhone: " + ci.phone +
"\n" );
}
System.out.println( "\n\n" );
}
}
// Return a result set of Java objects
// and cast them correctly.
private static void serializeColumnCastClass() throws Exception {
Statement stmt;
ResultSet rs;
byte arrayb[];
asademo.Product product;
asademo.Hat hat;
Object o;
String strClassname;
if ( conn!=null ) {
stmt = conn.createStatement();
rs = stmt.executeQuery( "SELECT sybase.sql.ASAUtils.toByteArray( jdba.Product.JProd ) FROM jdba.Product" );
while ( rs.next() ) {
arrayb = rs.getBytes(1);
o = (java.lang.Object)sybase.sql.ASAUtils.fromByteArray( arrayb );
strClassname = (o.getClass()).getName();
if ( strClassname.equals( "asademo.Product") ) {
product = (asademo.Product)o;
System.out.println( "This is a product: " + product.name );
} else if ( strClassname.equals( "asademo.Hat" ) ) {
hat = (asademo.Hat)o;
System.out.println( "The size of this hat is: " + hat.size );
}
}
}
}
///////////////////////////////////////////////
// Connect to an Apaptive Server Anywhere
// database server
///////////////////////////////////////////////
private static void ASAConnect( String UserID,
String Password,
String Machinename ) {
// uses global Connection variable
String _coninfo = new String( Machinename );
Properties _props = new Properties();
_props.put( "user", UserID );
_props.put( "password", Password );
// Load the Sybase Driver
try {
Class.forName( "com.sybase.jdbc.SybDriver" ).newInstance();
StringBuffer temp = new StringBuffer();
// Use the Sybase jConnect driver...
temp.append( "jdbc:sybase:Tds:" );
// to connect to the supplied machine name...
temp.append( _coninfo );
// on the default port number for ASA...
temp.append( ":2638" );
// and connect.
System.out.println( temp.toString() );
conn = DriverManager.getConnection( temp.toString() , _props );
}
catch ( Exception e ) {
System.out.println( "Error: " + e.getMessage() );
e.printStackTrace();
}
}
public static void InternalConnect() {
try {
conn = DriverManager.getConnection("");
System.out.println("Hello World");
}
catch ( Exception e ) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void InsertFixed() throws SQLException {
// returns current connection
conn = DriverManager.getConnection("");
// Disable autocommit
conn.setAutoCommit( false );
Statement stmt = conn.createStatement();
Integer IRows = new Integer( stmt.executeUpdate
("INSERT INTO Department (dept_id, dept_name )"
+ "VALUES (201, 'Eastern Sales')"
) );
// Print the number of rows updated
System.out.println(IRows.toString() + " row inserted" );
}
public static void DeleteFixed() {
try {
// returns current connection
conn = DriverManager.getConnection("");
// Disable autocommit
conn.setAutoCommit( false );
Statement stmt = conn.createStatement();
String sqlStr = "DELETE FROM Department "
+ "WHERE dept_id BETWEEN 201 and 299" ;
Integer IRows = new Integer( stmt.executeUpdate( sqlStr ) );
// Print the number of rows updated
System.out.println(IRows.toString() + " row deleted" );
}
catch ( Exception e ) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void InsertArguments(String id, String name) {
try {
conn = DriverManager.getConnection("");
// Disable autocommit
conn.setAutoCommit( false );
// Build the INSERT statement
String sqlStr = "INSERT INTO Department "
+ " ( dept_id, dept_name )"
+ " VALUES (" + id + ", '" + name + "')";
// Execute the statement
Statement stmt = conn.createStatement();
Integer IRows = new Integer( stmt.executeUpdate( sqlStr.toString() ) );
// Print the number of rows updated
System.out.println(IRows.toString() + " row inserted" );
}
catch ( Exception e ) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static void InsertPrepared(int id, String name) {
try {
conn = DriverManager.getConnection("");
// Disable autocommit
conn.setAutoCommit( false );
// Build the INSERT statement
// ? is a placeholder character
String sqlStr = "INSERT INTO Department "
+ "( dept_id, dept_name ) "
+ "VALUES ( ? , ? )" ;
// Prepare the statement
PreparedStatement stmt = conn.prepareStatement( sqlStr );
stmt.setInt(1, id);
stmt.setString(2, name );
Integer IRows = new Integer( stmt.executeUpdate() );
// Print the number of rows updated
System.out.println(IRows.toString() + " row inserted" );
}
catch ( Exception e ) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
public static int Query () {
int max_price = 0;
try{
conn = DriverManager.getConnection( "" );
// Build the query
String sqlStr = "SELECT id, unit_price "
+ "FROM product" ;
// Execute the statement
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery( sqlStr );
while( result.next() ) {
int price = result.getInt(2);
System.out.println( "Price is " + price );
if( price > max_price ) {
max_price = price ;
}
}
}
catch( Exception e ) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
return max_price;
}
}