FileDocCategorySizeDatePackage
ConnectToDB.javaAPI DocExample7383Mon Oct 16 19:44:06 BST 2000None

ConnectToDB

public class ConnectToDB extends Object

Fields Summary
Constructors Summary
Methods Summary
static voiddisplayResults(java.sql.ResultSet results)

  String line = "| ";
      try
      {  ResultSetMetaData rsmd = results.getMetaData();
         int numCols = rsmd.getColumnCount();
         int col;
         
         // Column headings
         for( col = 1; col <= numCols; col += 1 )
         {  if( col > 1 )
            {  line += " | ";
            }
            line += rsmd.getColumnLabel( col );
            for( int i = 0;
                 i < ( rsmd.getColumnDisplaySize( col ) - rsmd.getColumnLabel( col ).length() );
                 i += 1 )
            {  line += " ";
            }
         }
         line += " |";
         int tableWidth = line.length();
         for( int i = 0; i < tableWidth; i += 1 )
         {  System.out.print( "-" );
         }
         System.out.println();
         System.out.println( line );
         for( int i = 0; i < tableWidth; i += 1 )
         {  System.out.print( "-" );
         }
         System.out.println();
         
         // Data rows
         int rows = 0;
         while( results.next() == true )
         {  rows += 1;
            line = "| ";
            for( col = 1; col <= numCols; col += 1 )
            {  if( col > 1 )
               {  line += " | ";
               }
               line += results.getString( col );
               for( int i = 0;
                    i < ( rsmd.getColumnDisplaySize( col ) - results.getString( col ).length() );
                    i += 1 )
               {  line += " ";
               }
            }
            line += " |";
            System.out.println( line );
         }
         for( int i = 0; i < tableWidth; i += 1 )
         {  System.out.print( "-" );
         }
         System.out.println();
         System.out.println( "" + rows + " rows returned.\n" );
      }             
      catch( SQLException e )
      {  System.out.println( "Exception in Processing Results" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
   
public static voidmain(java.lang.String[] args)

  System.out.println( "Register driver: postgresql.Driver" );
      try
/**/  {  new postgresql.Driver();
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Driver Registration" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      
      System.out.println( "Get Connection to: jdbc:postgresql://ies11-49/ucjava" );
/**/  Connection con = null;
      try
/**/  {  con = DriverManager.getConnection( "jdbc:postgresql://ies11-49/ucjava", "ucjava", "" );
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Getting Connection" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      System.out.println( "Check for warnings from Connection" );
      SQLWarning warning = new SQLWarning();
      try
      {  warning = con.getWarnings();
         while( warning != null )
         {  System.out.println( "Warning: " + warning );
            warning = warning.getNextWarning();
         }
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Getting Connection Warnings" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      
      System.out.println( "Create a statement" );
/**/  Statement stmt = null;
      try
/**/  {  stmt = con.createStatement();
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Creating a Statement" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      
      System.out.println( "Execute query: SELECT * FROM students;" );
/**/  ResultSet results = null;
      try
/**/  {  results = stmt.executeQuery( "SELECT * FROM students;" );
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Executing Query" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }

      System.out.println( "Process Results\n" );
/**/  displayResults( results );     

      System.out.println( "Execute update: INSERT INTO students;" );
      try
/**/  {  int count = stmt.executeUpdate( "INSERT INTO students VALUES( 'Paul', 'Thompson', 'iq1b3482' );" );
         System.out.println( "" + count + " updates" );
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Executing Update" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }

      // Repeat query
      System.out.println( "Execute query: SELECT * FROM students;" );
      try
/**/  {  results = stmt.executeQuery( "SELECT * FROM students;" );
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Executing Query" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }

      System.out.println( "Process Results\n" );
/**/  displayResults( results );
      
      System.out.println( "Create a Prepared Statement: SELECT * FROM students WHERE firstname = ?;" );
/**/  PreparedStatement pStmt = null;
      try
/**/  {  pStmt = con.prepareStatement( "SELECT * FROM students WHERE firstname = ?;" );
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Creating a Prepared Statement" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      
      System.out.println( "Execute Prepared Statement with parameter: 'John'" );
      try
/**/  {  pStmt.setString( 1, "John" );
/**/     results = pStmt.executeQuery();
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Executing a Prepared Statement" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      System.out.println( "Process Results\n" );
/**/  displayResults( results );

      System.out.println( "Execute Prepared Statement with parameter: 'Paul'" );
      try
      {  pStmt.setString( 1, "Paul" );
         results = pStmt.executeQuery();
      }
      catch( SQLException e )
      {  System.out.println( "Exception in Executing a Prepared Statement" );
         while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
      System.out.println( "Process Results\n" );
/**/  displayResults( results );