home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / DBServ / SAMPLES / SAMPLES.ZIP / JDBCCALL / jdbcCall.java next >
Encoding:
Java Source  |  1997-02-21  |  8.4 KB  |  252 lines

  1. //
  2. // Copyright (c) 1996 Symantec.  All Rights Reserved.                                              *
  3. //
  4. // jdbcCall
  5. //
  6. // Author - Ron Emrick
  7. // Date   - 2/1/97
  8. //
  9. // jdbcCall demonstrates how to make stored procedure calls using
  10. // JDBC's CallableStatement class.
  11. //
  12. // Steps:
  13. //   1) Start the Sybase SQL Anywhere 5.0 engine
  14. //   2) Type at the DOS prompt: java jdbcCall
  15. //   3) Watch the output
  16. //
  17. // These stored procedures are designed to work with Sybase SQL Anywhere.
  18. // You should have little trouble porting them to other MS SQL Server,
  19. // Oracle or Sybase SQL Server.  The stored procedure code can be found
  20. // in the comments above each callXXX() method.
  21. //
  22.  
  23. import java.util.Properties;
  24.  
  25. import symjava.sql.*;
  26.  
  27. //////////////////////////////////////////////////////////////////////////
  28. //
  29. // jdbcCall
  30. //
  31. //////////////////////////////////////////////////////////////////////////
  32.  
  33. class jdbcCall extends JDBCShell
  34. {
  35.     ///////////////////////////////////////////////////////////////////////////
  36.     public static void main( String args[] )
  37.     {
  38.         jdbcCall jCall = new jdbcCall();
  39.         jCall.shellMain( args );
  40.     }
  41.  
  42.     ///////////////////////////////////////////////////////////////////////////
  43.     protected void init()
  44.     {
  45.         m_strAppName = "jdbcCall";
  46.     }
  47.  
  48.     ///////////////////////////////////////////////////////////////////////////
  49.     public void execute()
  50.     {
  51.        callInt( m_nTestExecutions );
  52.        callVarChar( m_nTestExecutions );
  53.        callDate( m_nTestExecutions );
  54.        callDouble( m_nTestExecutions );
  55.     }
  56.  
  57.     ///////////////////////////////////////////////////////////////////////////
  58.     //
  59.     // create procedure "dba".sp_callint(@inVal integer,@outVal integer output) as
  60.     // begin
  61.     //    select @outVal=@inVal*2
  62.     // end
  63.     //
  64.     public void callInt( int iterations )
  65.     {
  66.         try {
  67.             // Drop existing stored procedure
  68.             System.out.println( "Dropping existing stored procedure..." );
  69.             Statement jdbcStmt = m_jdbcConn.createStatement();
  70.             try {
  71.                 jdbcStmt.execute( "drop procedure sp_callint" );
  72.             }
  73.             catch( SQLException x ) {
  74.             }
  75.  
  76.             // Create new stored procedure
  77.             System.out.println( "Creating new stored procedure..." );
  78.             jdbcStmt.execute( "create procedure \"dba\".sp_callint(@inVal integer,@outVal integer output) as " +
  79.                               "begin select @outVal=@inVal*2 end" );
  80.             jdbcStmt.close();
  81.  
  82.             // Execute CallableStatement
  83.             System.out.println( "Executing stored procedure..." );
  84.             CallableStatement cs = m_jdbcConn.prepareCall( "{ call sp_callint( ?, ? ) }" );
  85.             cs.registerOutParameter( 2, Types.INTEGER );
  86.  
  87.             int seed = 10;
  88.             for ( int i = 0; i < iterations; i++ ) {
  89.                 cs.setInt( 1, seed + i );
  90.  
  91.                 cs.execute();
  92.  
  93.                 int result = cs.getInt( 2 );
  94.                 System.out.println( "Integer Output = " + result );
  95.             }
  96.  
  97.             cs.close();
  98.         }
  99.         catch( SQLException x ) {
  100.             handleException( x, "SQLException", "" );
  101.             return;
  102.         }
  103.     }
  104.  
  105.     ///////////////////////////////////////////////////////////////////////////
  106.     //
  107.     // create procedure sp_callvc(@inVal varchar(50),@outVal varchar(50) output)
  108.     // begin
  109.     //   select @outVal=@inVal+@inVal
  110.     // end
  111.     //
  112.     public void callVarChar( int iterations )
  113.     {
  114.         try {
  115.             // Drop existing stored procedure
  116.             System.out.println( "Dropping existing stored procedure..." );
  117.             Statement jdbcStmt = m_jdbcConn.createStatement();
  118.             try {
  119.                 jdbcStmt.execute( "drop procedure sp_callvc" );
  120.             }
  121.             catch( SQLException x ) {
  122.             }
  123.  
  124.             // Create new stored procedure
  125.             System.out.println( "Creating new stored procedure..." );
  126.             jdbcStmt.execute( "create procedure sp_callvc(@inVal varchar(50),@outVal varchar(50) output) " +
  127.                               "begin select @outVal=@inVal+@inVal end" );
  128.             jdbcStmt.close();
  129.  
  130.             // Execute CallableStatement
  131.             CallableStatement cs = m_jdbcConn.prepareCall( "{ call sp_callvc( ?, ? ) }" );
  132.             cs.registerOutParameter( 2, Types.VARCHAR );
  133.  
  134.             String seed = new String( "dbANYWHERE Server is cool stuff!" );
  135.             for ( int i = 0; i < iterations; i++ ) {
  136.                 cs.setString( 1, seed );
  137.  
  138.                 cs.execute();
  139.  
  140.                 String result = cs.getString( 2 );
  141.                 System.out.println( "VarChar Output = " + result );
  142.             }
  143.  
  144.             cs.close();
  145.         }
  146.         catch( SQLException x ) {
  147.             handleException( x, "SQLException", "" );
  148.             return;
  149.         }
  150.     }
  151.  
  152.     ///////////////////////////////////////////////////////////////////////////
  153.     //
  154.     // create procedure sp_calldate(@inVal date,@outVal date output) as
  155.     // begin
  156.     //   select @outVal=months(@inVal,1)
  157.     //   select @outVal=days(@outVal,1)
  158.     //   select @outVal=years(@outVal,1)
  159.     // end
  160.     //
  161.     public void callDate( int iterations )
  162.     {
  163.         try {
  164.             // Drop existing stored procedure
  165.             System.out.println( "Dropping existing stored procedure..." );
  166.             Statement jdbcStmt = m_jdbcConn.createStatement();
  167.             try {
  168.                 jdbcStmt.execute( "drop procedure sp_calldate" );
  169.             }
  170.             catch( SQLException x ) {
  171.             }
  172.  
  173.             // Create new stored procedure
  174.             System.out.println( "Creating new stored procedure..." );
  175.             jdbcStmt.execute( "create procedure sp_calldate(@inVal date,@outVal date output) as " +
  176.                               "begin " +
  177.                               "select @outVal=months(@inVal,1) " +
  178.                               "select @outVal=days(@outVal,1) " +
  179.                               "select @outVal=years(@outVal,1) " +
  180.                               "end " );
  181.             jdbcStmt.close();
  182.  
  183.             // Execute CallableStatement
  184.             CallableStatement cs = m_jdbcConn.prepareCall( "{ call sp_calldate( ?, ? ) }" );
  185.             cs.registerOutParameter( 2, Types.DATE );
  186.  
  187.             Date seed = new Date( 97, 1, 1 );
  188.             for ( int i = 0; i < iterations; i++ ) {
  189.                 cs.setDate( 1, seed );
  190.  
  191.                 cs.execute();
  192.  
  193.                 Date result = cs.getDate( 2 );
  194.                 System.out.println( "Date Output = " + result.toString() );
  195.             }
  196.  
  197.             cs.close();
  198.         }
  199.         catch( SQLException x ) {
  200.             handleException( x, "SQLException", "" );
  201.             return;
  202.         }
  203.     }
  204.  
  205.     ///////////////////////////////////////////////////////////////////////////
  206.     //
  207.     // create procedure sp_calldouble(@inVal double,@outVal double output) as
  208.     // begin
  209.     //   select @outVal=@inVal+3.1415
  210.     // end
  211.     //
  212.     public void callDouble( int iterations )
  213.     {
  214.         try {
  215.             // Drop existing stored procedure
  216.             System.out.println( "Dropping existing stored procedure..." );
  217.             Statement jdbcStmt = m_jdbcConn.createStatement();
  218.             try {
  219.                 jdbcStmt.execute( "drop procedure sp_calldouble" );
  220.             }
  221.             catch( SQLException x ) {
  222.             }
  223.  
  224.             // Create new stored procedure
  225.             System.out.println( "Creating new stored procedure..." );
  226.             jdbcStmt.execute( "create procedure sp_calldouble(@inVal double,@outVal double output) as " +
  227.                               "begin select @outVal=@inVal+3.1415 end" );
  228.             jdbcStmt.close();
  229.  
  230.             // Execute CallableStatement
  231.             CallableStatement cs = m_jdbcConn.prepareCall( "{ call sp_calldouble( ?, ? ) }" );
  232.             cs.registerOutParameter( 2, Types.DOUBLE );
  233.  
  234.             double seed = 10;
  235.             for ( int i = 0; i < iterations; i++ ) {
  236.                 cs.setDouble( 1, seed + i );
  237.  
  238.                 cs.execute();
  239.  
  240.                 double result = cs.getDouble( 2 );
  241.                 System.out.println( "Double Output = " + result );
  242.             }
  243.  
  244.             cs.close();
  245.         }
  246.         catch( SQLException x ) {
  247.             handleException( x, "SQLException", "" );
  248.             return;
  249.         }
  250.     }
  251.  
  252. } // class jdbcCall