A simple JDBC Execute

Home  |   Query with JDBC4  -   Query  -   Update  -   Execute  -   MetaData   -   PreparedStatement   -   CallableStatement   -   BlueJ  
/******************************************************************************/
/*                                                                            */
/*                                                     FILE: JDBCExecute.java */
/*                                                                            */
/*  A simple JDBC EXECUTE                                                     */
/*  =====================                                                     */
/*                                                                            */
/*  V1.00   24-JAN-1999   Te          https://www.heimetli.ch                 */
/*                                                                            */
/*  ------------------------------------------------------------------------- */
/*                                                                            */
/*  This example was coded and tested with JDK1.2.2                           */
/*                                                                            */
/*  Access was used as database, over the JDBC-ODBC bridge which comes        */
/*  with the JDK.                                                             */
/*                                                                            */
/*  To run this example, you need a database with the following properties:   */
/*  => no username                                                            */
/*  => no password                                                            */
/*  => a system DSN called "Database"                                         */
/*                                                                            */
/******************************************************************************/

import java.sql.* ;

class JDBCExecute
{
 public static void main( String args[] )
 {
  if( args.length != 1 )
  {
      System.out.println( "usage: java JDBCExecute sql" ) ;
      return ;
  }

  try
  {
      /***********************************************************/
      /*                                                         */
      /*  Since JDBC4 the following line is no longer necessary. */
      /*                                                         */
      /*  See the comments in JDBC4Query.html                    */
      /*                                                         */
      /***********************************************************/
      // Load the database driver
      Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;

      // Get a connection to the database
      Connection conn = DriverManager.getConnection( "jdbc:odbc:Database" ) ;

      // Print all warnings
      for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
      {
          System.out.println( "SQL Warning:" ) ;
          System.out.println( "State  : " + warn.getSQLState()  ) ;
          System.out.println( "Message: " + warn.getMessage()   ) ;
          System.out.println( "Error  : " + warn.getErrorCode() ) ;
      }

      // Get a statement from the connection
      Statement stmt = conn.createStatement() ;

      // Execute the SQL
      if( stmt.execute(args[0]) == false )
      {
          // Get the update count
          int num = stmt.getUpdateCount() ;

          System.out.println( num + " rows affected" ) ;
      }
      else
      {
          // Get the result set and the metadata
          ResultSet         rs = stmt.getResultSet() ;
          ResultSetMetaData md = rs.getMetaData() ;

          // Loop through the result set
          while( rs.next() )
          {
              for( int i = 1; i <= md.getColumnCount(); i++ )
                 System.out.print( rs.getString(i) + " " ) ;
              System.out.println() ;
          }

          // Close the result set, statement and the connection
          rs.close() ;
      }

      stmt.close() ;
      conn.close() ;
  }
  catch( SQLException se )
  {
      System.out.println( "SQL Exception:" ) ;

      // Loop through the SQL Exceptions
      while( se != null )
      {
          System.out.println( "State  : " + se.getSQLState()  ) ;
          System.out.println( "Message: " + se.getMessage()   ) ;
          System.out.println( "Error  : " + se.getErrorCode() ) ;

          se = se.getNextException() ;
      }
  }
  catch( Exception e )
  {
      System.out.println( e ) ;
  }
 }
}
Download Source