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 / JDBCDML / jdbcDML.java next >
Encoding:
Java Source  |  1997-02-21  |  6.5 KB  |  216 lines

  1. //
  2. // Copyright (c) 1996 Symantec.  All Rights Reserved.                                              *
  3. //
  4. // jdbcDML
  5. //
  6. // Author - Ron Emrick
  7. // Date   - 2/1/97
  8. //
  9. // Description:
  10. //   jdbcDML demonstrates how to perform SQL DML using JDBC's
  11. //   PreparedStatement class.
  12. //
  13. // Steps:
  14. //   1) Type at the DOS prompt: java jdbcDML or java jdbcDML -t <tablename>
  15. //   2) Watch the output
  16. //
  17.  
  18. import java.util.Properties;
  19.  
  20. import symjava.sql.*;
  21.  
  22. //////////////////////////////////////////////////////////////////////////
  23. //
  24. // jdbcDML
  25. //
  26. //////////////////////////////////////////////////////////////////////////
  27.  
  28. class jdbcDML extends jdbcViewer
  29. {
  30.     ///////////////////////////////////////////////////////////////////////////
  31.     public static void main( String args[] )
  32.     {
  33.         jdbcDML jCall = new jdbcDML();
  34.         jCall.shellMain( args );
  35.     }
  36.  
  37.     ///////////////////////////////////////////////////////////////////////////
  38.     protected void init()
  39.     {
  40.         m_strAppName = "jdbcDML";
  41.         m_strTable   = "dbaw_jdbc_dml";
  42.     }
  43.  
  44.     ///////////////////////////////////////////////////////////////////////////
  45.     public void execute()
  46.     {
  47.         prepareDML();
  48.  
  49.         performInsert( m_nTestExecutions );
  50.         performUpdate( m_nTestExecutions );
  51.         performDelete( m_nTestExecutions );
  52.     }
  53.  
  54.     ///////////////////////////////////////////////////////////////////////////
  55.     public void prepareDML()
  56.     {
  57.         try {
  58.             // Drop existing table
  59.  
  60.             Statement jdbcStmt = m_jdbcConn.createStatement();
  61.  
  62.             try {
  63.                 jdbcStmt.execute( "drop table " + m_strTable );
  64.             }
  65.             catch( SQLException x ) {
  66.             }
  67.  
  68.             // Create new table
  69.             jdbcStmt.execute( "create table " + m_strTable + " ( " +
  70.                               "cust_id int primary key, name varchar(100), hobby varchar(50) )" );
  71.  
  72.             // Close statements when you no longer need them!
  73.             jdbcStmt.close();
  74.  
  75.             // Turn OFF auto-commit - JDBC defaults to auto-commit ON
  76.             m_jdbcConn.setAutoCommit( false );
  77.         }
  78.         catch( SQLException x ) {
  79.             handleException( x, "SQLException", "" );
  80.             return;
  81.         }
  82.     }
  83.  
  84.     ///////////////////////////////////////////////////////////////////////////
  85.     public void performInsert( int iterations )
  86.     {
  87.         PreparedStatement jdbcPStmt = null;
  88.  
  89.         try {
  90.             System.out.println( "** INSERTING " + iterations + " ROW(S) **" );
  91.  
  92.             jdbcPStmt = m_jdbcConn.prepareStatement( "insert into " + m_strTable + " values ( ?, ?, ? )" );
  93.             for ( int i = 1; i <= iterations; i++ ) {
  94.                 jdbcPStmt.setInt( 1, i );
  95.                 jdbcPStmt.setString( 2, "dbANYWHERE Person #" + i );
  96.                 jdbcPStmt.setString( 3, "dbANYWHERE Hobby #" + i );
  97.  
  98.                 System.out.println( "\tinserting row " + i );
  99.                 jdbcPStmt.execute();
  100.             }
  101.             jdbcPStmt.close();
  102.         }
  103.         catch( SQLException x ) {
  104.             closeStatement( jdbcPStmt );
  105.             rollbackTransaction();
  106.             handleException( x, "SQLException", "Record insert failed" );
  107.             return;
  108.         }
  109.  
  110.         commitTransaction();
  111.  
  112.         // View the contents
  113.         tableViewer( 1 );
  114.     }
  115.  
  116.     ///////////////////////////////////////////////////////////////////////////
  117.     public void performUpdate( int iterations )
  118.     {
  119.         PreparedStatement jdbcPStmt = null;
  120.  
  121.         try {
  122.             System.out.println( "** UPDATING " + iterations + " ROW(S) **" );
  123.  
  124.             jdbcPStmt = m_jdbcConn.prepareStatement( "update " + m_strTable +
  125.                                                      " set name = ? where cust_id = ?" );
  126.             for ( int i = 1; i <= iterations; i++ ) {
  127.                 jdbcPStmt.setString( 1, "dbANYWHERE Person #" + ( i * 2 ) );
  128.                 jdbcPStmt.setInt( 2, i );
  129.  
  130.                 System.out.println( "\tupdating row " + i );
  131.                 jdbcPStmt.execute();
  132.             }
  133.             jdbcPStmt.close();
  134.         }
  135.         catch( SQLException x ) {
  136.             closeStatement( jdbcPStmt );
  137.             rollbackTransaction();
  138.             handleException( x, "SQLException", "Record update failed" );
  139.             return;
  140.         }
  141.  
  142.         commitTransaction();
  143.  
  144.         // View the contents
  145.         tableViewer( 1 );
  146.     }
  147.  
  148.     ///////////////////////////////////////////////////////////////////////////
  149.     public void performDelete( int iterations )
  150.     {
  151.         PreparedStatement jdbcPStmt = null;
  152.  
  153.         try {
  154.             System.out.println( "** DELETING " + iterations + " ROW(S) **" );
  155.  
  156.             jdbcPStmt = m_jdbcConn.prepareStatement( "delete from " + m_strTable + " where cust_id = ?" );
  157.             for ( int i = 1; i <= iterations; i++ ) {
  158.                 jdbcPStmt.setInt( 1, i );
  159.  
  160.                 System.out.println( "\tdeleting row " + i );
  161.                 jdbcPStmt.execute();
  162.             }
  163.             jdbcPStmt.close();
  164.         }
  165.         catch( SQLException x ) {
  166.             closeStatement( jdbcPStmt );
  167.             rollbackTransaction();
  168.             handleException( x, "SQLException", "Record deletion failed" );
  169.             return;
  170.         }
  171.  
  172.         commitTransaction();
  173.  
  174.         // View the contents
  175.         tableViewer( 1 );
  176.     }
  177.  
  178.     ///////////////////////////////////////////////////////////////////////////
  179.     protected void commitTransaction()
  180.     {
  181.         try {
  182.             m_jdbcConn.commit();
  183.         }
  184.         catch( SQLException x ) {
  185.             handleException( x, "SQLException", "Commit failed" );
  186.             return;
  187.         }
  188.     }
  189.  
  190.     ///////////////////////////////////////////////////////////////////////////
  191.     protected void rollbackTransaction()
  192.     {
  193.         try {
  194.             m_jdbcConn.rollback();
  195.         }
  196.         catch( SQLException x ) {
  197.             handleException( x, "SQLException", "Rollback failed" );
  198.             return;
  199.         }
  200.     }
  201.  
  202.     ///////////////////////////////////////////////////////////////////////////
  203.     protected void closeStatement( Statement jdbcStmt )
  204.     {
  205.         try {
  206.             if ( jdbcStmt != null ) {
  207.                 jdbcStmt.close();
  208.             }
  209.         }
  210.         catch( SQLException x ) {
  211.             handleException( x, "SQLException", "Statement close failed" );
  212.             return;
  213.         }
  214.     }
  215.  
  216. } // class jdbcDML