home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 6.5 KB | 216 lines |
- //
- // Copyright (c) 1996 Symantec. All Rights Reserved. *
- //
- // jdbcDML
- //
- // Author - Ron Emrick
- // Date - 2/1/97
- //
- // Description:
- // jdbcDML demonstrates how to perform SQL DML using JDBC's
- // PreparedStatement class.
- //
- // Steps:
- // 1) Type at the DOS prompt: java jdbcDML or java jdbcDML -t <tablename>
- // 2) Watch the output
- //
-
- import java.util.Properties;
-
- import symjava.sql.*;
-
- //////////////////////////////////////////////////////////////////////////
- //
- // jdbcDML
- //
- //////////////////////////////////////////////////////////////////////////
-
- class jdbcDML extends jdbcViewer
- {
- ///////////////////////////////////////////////////////////////////////////
- public static void main( String args[] )
- {
- jdbcDML jCall = new jdbcDML();
- jCall.shellMain( args );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void init()
- {
- m_strAppName = "jdbcDML";
- m_strTable = "dbaw_jdbc_dml";
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void execute()
- {
- prepareDML();
-
- performInsert( m_nTestExecutions );
- performUpdate( m_nTestExecutions );
- performDelete( m_nTestExecutions );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void prepareDML()
- {
- try {
- // Drop existing table
-
- Statement jdbcStmt = m_jdbcConn.createStatement();
-
- try {
- jdbcStmt.execute( "drop table " + m_strTable );
- }
- catch( SQLException x ) {
- }
-
- // Create new table
- jdbcStmt.execute( "create table " + m_strTable + " ( " +
- "cust_id int primary key, name varchar(100), hobby varchar(50) )" );
-
- // Close statements when you no longer need them!
- jdbcStmt.close();
-
- // Turn OFF auto-commit - JDBC defaults to auto-commit ON
- m_jdbcConn.setAutoCommit( false );
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "" );
- return;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void performInsert( int iterations )
- {
- PreparedStatement jdbcPStmt = null;
-
- try {
- System.out.println( "** INSERTING " + iterations + " ROW(S) **" );
-
- jdbcPStmt = m_jdbcConn.prepareStatement( "insert into " + m_strTable + " values ( ?, ?, ? )" );
- for ( int i = 1; i <= iterations; i++ ) {
- jdbcPStmt.setInt( 1, i );
- jdbcPStmt.setString( 2, "dbANYWHERE Person #" + i );
- jdbcPStmt.setString( 3, "dbANYWHERE Hobby #" + i );
-
- System.out.println( "\tinserting row " + i );
- jdbcPStmt.execute();
- }
- jdbcPStmt.close();
- }
- catch( SQLException x ) {
- closeStatement( jdbcPStmt );
- rollbackTransaction();
- handleException( x, "SQLException", "Record insert failed" );
- return;
- }
-
- commitTransaction();
-
- // View the contents
- tableViewer( 1 );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void performUpdate( int iterations )
- {
- PreparedStatement jdbcPStmt = null;
-
- try {
- System.out.println( "** UPDATING " + iterations + " ROW(S) **" );
-
- jdbcPStmt = m_jdbcConn.prepareStatement( "update " + m_strTable +
- " set name = ? where cust_id = ?" );
- for ( int i = 1; i <= iterations; i++ ) {
- jdbcPStmt.setString( 1, "dbANYWHERE Person #" + ( i * 2 ) );
- jdbcPStmt.setInt( 2, i );
-
- System.out.println( "\tupdating row " + i );
- jdbcPStmt.execute();
- }
- jdbcPStmt.close();
- }
- catch( SQLException x ) {
- closeStatement( jdbcPStmt );
- rollbackTransaction();
- handleException( x, "SQLException", "Record update failed" );
- return;
- }
-
- commitTransaction();
-
- // View the contents
- tableViewer( 1 );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void performDelete( int iterations )
- {
- PreparedStatement jdbcPStmt = null;
-
- try {
- System.out.println( "** DELETING " + iterations + " ROW(S) **" );
-
- jdbcPStmt = m_jdbcConn.prepareStatement( "delete from " + m_strTable + " where cust_id = ?" );
- for ( int i = 1; i <= iterations; i++ ) {
- jdbcPStmt.setInt( 1, i );
-
- System.out.println( "\tdeleting row " + i );
- jdbcPStmt.execute();
- }
- jdbcPStmt.close();
- }
- catch( SQLException x ) {
- closeStatement( jdbcPStmt );
- rollbackTransaction();
- handleException( x, "SQLException", "Record deletion failed" );
- return;
- }
-
- commitTransaction();
-
- // View the contents
- tableViewer( 1 );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void commitTransaction()
- {
- try {
- m_jdbcConn.commit();
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "Commit failed" );
- return;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void rollbackTransaction()
- {
- try {
- m_jdbcConn.rollback();
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "Rollback failed" );
- return;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void closeStatement( Statement jdbcStmt )
- {
- try {
- if ( jdbcStmt != null ) {
- jdbcStmt.close();
- }
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "Statement close failed" );
- return;
- }
- }
-
- } // class jdbcDML