FileDocCategorySizeDatePackage
DBTable.javaAPI DocExample3301Mon Oct 16 19:44:06 BST 2000None

DBTable.java

// File: DBTable.java
// T Balls : Nov 1999
// JDBC and JTable demo

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

public class DBTable extends JFrame
{  public static void main( String[] args )
   {  new DBTable();
   }

   private String driver = "postgresql.Driver";
   private String dbURL = "jdbc:postgresql://localhost/ucjava";
   private String loginName = "ucjava";
   private String password ="ucJava";

   private JTextArea query = new JTextArea( 3, 50 );
   private JButton submit = new JButton( "Submit Query" );

   public DBTable()
   {  super( "JDBC + JTable" );
      connect();
      getContentPane().add( query, BorderLayout.NORTH );
      getContentPane().add( submit, BorderLayout.SOUTH );

      submit.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e )
         {  performQuery();
         }
      });

      addWindowListener( new WindowAdapter() {
         public void windowClosing( WindowEvent e )
         {  System.exit(0);
         }
      });
      pack();
      setVisible(true);

   }

   private Connection con = null;
   private Statement stmt = null;
   /** connect to database
   */
   private void connect()
   {  try
      {  Class.forName( driver );
         con = DriverManager.getConnection( dbURL, loginName, password );
         stmt = con.createStatement();
      }
      catch( ClassNotFoundException cnf )
      {  cnf.printStackTrace();
         System.exit(0);
      }
      catch( SQLException e )
      {  while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         System.exit(0);
      }
   }

   JTable table = null;
   JScrollPane scroller = null;
   /** send contents of TextArea to database
   */
   private void performQuery()
   {  ResultSet res = null;
      try
      {  res = stmt.executeQuery( query.getText() );
      }
      catch( SQLException e )
      {  while( e != null )
         {  System.out.println( e );
            e = e.getNextException();
         }
         return;
      }
      Vector header = null;
      Vector rows = null;
      try
      {  ResultSetMetaData rsmd = res.getMetaData();
         int cols = rsmd.getColumnCount();
         header = new Vector();
         int col;
         for( col = 1; col <= cols; col++ )
         {  header.add( rsmd.getColumnLabel( col ) );
         }
         rows = new Vector();
         while( res.next() == true )
         {  Vector row = new Vector();
            for( col=1; col <= cols; col++ )
            {  row.add( res.getString( col ) );
            }
            rows.add( row );
         }
      }
      catch( SQLException e )
      {  while( e != null )
         {  e.printStackTrace();
            e = e.getNextException();
         }
         return;
      }
      if( scroller != null )
      {  getContentPane().remove( scroller );
      }
      table = new JTable( rows, header );
      scroller = new JScrollPane( table );
      table.setPreferredScrollableViewportSize( new Dimension( 300, 200 ) );
      getContentPane().add( scroller, BorderLayout.CENTER );
      pack();
   }
}