A simple JDBC Query for JDBC4

Home  |   Query with JDBC4  -   Query  -   Update  -   Execute  -   MetaData   -   PreparedStatement   -   CallableStatement   -   BlueJ  
/******************************************************************************/
/*                                                                            */
/*                                                      FILE: JDBC4Query.java */
/*                                                                            */
/*  An improved version of the simple JDBC Query example                      */
/*  ====================================================                      */
/*                                                                            */
/*  V1.00   30-MAY-2016   Te          https://www.heimetli.ch/                */
/*                                                                            */
/*  ------------------------------------------------------------------------- */
/*                                                                            */
/*  Since JDBC version 4.0, the call to Class.forName is no longer necessary  */
/*  because the driver is loaded automatically from the jar(s) on the         */
/*  command line. Therefore, I updated the example to reflect this new        */
/*  behaviour.                                                                */
/*                                                                            */
/*  Another improvement is the use of try-with-resources blocks to free       */
/*  the resources when an error occurs.                                       */
/*                                                                            */
/*  This example was compiled and tested with JDK1.7.0_91 (OpenJDK) on Linux. */
/*                                                                            */
/*  It ran on a Linux machine with MySQL 5.5.46, and the JDBC driver was      */
/*  mysql-connector-java-5.1.39-bin.jar.                                      */
/*                                                                            */
/*  The command line was:                                                     */
/*                                                                            */
/*  java -classpath .:mysql-connector-java-5.1.39-bin.jar JDBC4Query          */
/*                                                                            */
/*  This program requires a database called 'Customers', having a table named */
/*  'Address' with at least one column. The MySQL username is 'User' and the  */
/*  password is 'Password'.                                                   */
/*                                                                            */
/******************************************************************************/

import java.sql.* ;

class JDBC4Query
{
   public static void main( String args[] )
   {
      // Get a connection to the database
      try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Customers?user=User&password=Password") )
      {
         // 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
         try( Statement stmt = conn.createStatement() )
         {
            // Execute the query
            try( ResultSet rs = stmt.executeQuery("SELECT * FROM Address") )
            {
               // Loop through the result set
               while( rs.next() )
                  System.out.println( rs.getString(1) ) ;
            }
         }
      }
      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