We help IT Professionals succeed at work.
Get Started

Read data from access database...

Mike Eghtebas
on
554 Views
Last Modified: 2012-08-14
Sample code exists to read data from an access database (books.mdb attached) in Windows 7 environment.

This code has some other information in it.

Question: How can I simplify the code just to read and display the content of author table in the database?

Also, is it necessary to create odbc connection in the computer I will be testing it? If so how to create such a connection?
 
Thank you.


import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

public class DisplayAuthors extends JFrame {
   
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
   static final String DATABASE_URL = "jdbc:odbc:books";
   
   // declare Connection and Statement for accessing 
   // and querying database
   private Connection connection;
   private Statement statement;
   
   // constructor connects to database, queries database, processes 
   // results and displays results in window
   public DisplayAuthors() 
   {
      super( "Authors Table of Books Database" );
      
      // connect to database books and query database
      try {
         
         // specify location of database on filesystem
         //System.setProperty( "db2j.system.home", "C:/Cloudscape_5.0" );
         
         // load database driver class
         Class.forName( JDBC_DRIVER );

         // establish connection to database
         connection = DriverManager.getConnection( DATABASE_URL );

         // create Statement for querying database
         statement = connection.createStatement();
         
         // query database
         ResultSet resultSet = 
            statement.executeQuery( "SELECT firstName, lastName, gpa FROM authors" );
         
         // process query results
         StringBuffer results = new StringBuffer();
         ResultSetMetaData metaData = resultSet.getMetaData();
         int numberOfColumns = metaData.getColumnCount();
         
         for ( int i = 1; i <= numberOfColumns; i++ )
            results.append( metaData.getColumnName( i ) + "\t" );
         
         results.append( "\n" );
         
         while ( resultSet.next() ) {
            
            for ( int i = 1; i <= numberOfColumns; i++ )
               results.append( resultSet.getObject( i ) + "\t" );
            
            results.append( "\n" );
         }

         // set up GUI and display window
         JTextArea textArea = new JTextArea( results.toString() );
         Container container = getContentPane();

         container.add( new JScrollPane( textArea ) );
         
         setSize( 300, 100 );  // set window size
         setVisible( true );   // display window

      }  // end try
      
      // detect problems interacting with the database
      catch ( SQLException sqlException ) {
         JOptionPane.showMessageDialog( null, sqlException.getMessage(), 
            "Database Error", JOptionPane.ERROR_MESSAGE );
         
         System.exit( 1 );
      }
      
      // detect problems loading database driver
      catch ( ClassNotFoundException classNotFound ) {
         JOptionPane.showMessageDialog( null, classNotFound.getMessage(), 
            "Driver Not Found", JOptionPane.ERROR_MESSAGE );            

         System.exit( 1 );
      }
      
      // ensure statement and connection are closed properly
      finally {
         
         try {
            statement.close();
            connection.close();            
         }
         
         // handle exceptions closing statement and connection
         catch ( SQLException sqlException ) {
            JOptionPane.showMessageDialog( null, 
               sqlException.getMessage(), "Database Error", 
               JOptionPane.ERROR_MESSAGE );
         
            System.exit( 1 );
         }
      }

   }  // end DisplayAuthors constructor
   
   // launch the application
   public static void main( String args[] )
   {
      DisplayAuthors window = new DisplayAuthors();      
      window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

}  // end class DisplayAuthors

Open in new window

books.mdb
Comment
Watch Question
Awarded 2011
Awarded 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 54 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE