Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

Custom Search
Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 6/9 



Previous Page Previous Page (5/9) - Next Page (7/9) Next Page
Subpages: 1. JTable  
2.
Stocks Table: part I - Basic JTable example 
3.
Stocks Table: part II - Custom renderers 
4. Stocks Table: part III - Data formatting 
5. Stocks Table: part IV - Sorting columns 
6.
Stocks Table: part V - JDBC
7.
Stocks Table: part VI - Column addition and removal
8.
Expense report application
9.
JavaBeans property editor

18.6  Stocks Table: part V - JDBC

Despite all of our sorting functionality and enhanced data display, our application is still quite boring because it displays only data for a pre-defined day! Of course, in the real world we need to connect such an application to the source of fresh information such as a database. Very often tables are used to display data retrieved from databases, or to edit data to be stored in databases. In this section we show how to feed our StocksTable data extracted from a database using the Java Database Connectivity (JDBC) API.

First, we need to create the database. We chose to use two SQL tables (do not confuse SQL table with JTable) whose structure precisely corresponds to the market data structure described in section 18.2:

Table SYMBOLS

Field Name            Type

symbol                   Text

name                       Text

Table DATA

Field Name            Type

symbol                   Text

date1                      Date/Time

last                          Number

change                   Number

changeproc           Number

open                       Number

volume                   Number

For this example we use the JDBC-ODBC bridge which is a standard part of JDK since the 1.1 release and links Java programs to Microsoft Access databases. If you are using another database engine, you can work with this example as well, but you must make sure that the structure of your tables is the same. Before running the example in a Windows environment we need to register a database in an ODBC Data Source Administrator which is accessible through the Control Panel (this is not a JDBC tutorial, so we'll skip the details).

Figure 18.5 Retrieving stock data from a database for display in JTable.

<<file figure18-5.gif>>

The Code: StocksTable.java

see \Chapter18\5

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import java.text.*;

import java.sql.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class StocksTable extends JFrame

{

  protected JTable m_table;

  protected StockTableData m_data;

  protected JLabel m_title;

  public StocksTable() {

    // Unchanged code from section 18.4

    JMenuBar menuBar = createMenuBar();

      setJMenuBar(menuBar);

    // Unchanged code

  }

  protected JMenuBar createMenuBar() {

    JMenuBar menuBar = new JMenuBar();

    JMenu mFile = new JMenu("File");

    mFile.setMnemonic('f');

    JMenuItem mData = new JMenuItem("Retrieve Data...");

    mData.setMnemonic('r');

    ActionListener lstData = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        retrieveData();

      }

    };

    mData.addActionListener(lstData);

    mFile.add(mData);

    mFile.addSeparator();

    JMenuItem mExit = new JMenuItem("Exit");

    mExit.setMnemonic('x');

    ActionListener lstExit = new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        System.exit(0);

      }

    };

    mExit.addActionListener(lstExit);

    mFile.add(mExit);

    menuBar.add(mFile);

    return menuBar;

  }

  public void retrieveData() {

    SimpleDateFormat frm = new SimpleDateFormat("MM/dd/yyyy");

    String currentDate = frm.format(m_data.m_date);

    String result = (String)JOptionPane.showInputDialog(this,

      "Please enter date in form mm/dd/yyyy:", "Input",

      JOptionPane.INFORMATION_MESSAGE, null, null,

      currentDate);

    if (result==null)

      return;

    java.util.Date date = null;

    try {

      date = frm.parse(result);

    }

    catch (java.text.ParseException ex) {

      date = null;

    }

    if (date == null) {

      JOptionPane.showMessageDialog(this,

        result+" is not a valid date",

        "Warning", JOptionPane.WARNING_MESSAGE);

      return;

    }

    setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) );

    switch (m_data.retrieveData(date)) {

      case 0:    // Ok with data

        m_title.setText(m_data.getTitle());

        m_table.tableChanged(new TableModelEvent(m_data));

        m_table.repaint();

        break;

      case 1: // No data

        JOptionPane.showMessageDialog(this,

          "No data found for "+result,

          "Warning", JOptionPane.WARNING_MESSAGE);

        break;

      case -1: // Error

        JOptionPane.showMessageDialog(this,

          "Error retrieving data",

          "Warning", JOptionPane.WARNING_MESSAGE);

        break;

    }

    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

  }

  public static void main(String argv[]) {

    new StocksTable();

  }

}

// Unchanged code from section 18.4

class StockTableData extends AbstractTableModel

{

  static final public ColumnData m_columns[] = {

    // Unchanged code from section 18.2

  };

  protected SimpleDateFormat m_frm;

  protected Vector m_vector;

  protected java.util.Date m_date; // conflict with

  protected int m_sortCol = 0;

  protected boolean m_sortAsc = true;

  protected int m_result = 0;

  public StockTableData() {

    m_frm = new SimpleDateFormat("MM/dd/yyyy");

    m_vector = new Vector();

    setDefaultData();

  }

  // Unchanged code from section 18.4

  public int retrieveData(final java.util.Date date) {

    GregorianCalendar calendar = new GregorianCalendar();

    calendar.setTime(date);

    int month = calendar.get(Calendar.MONTH)+1;

    int day = calendar.get(Calendar.DAY_OF_MONTH);

    int year = calendar.get(Calendar.YEAR);

    final String query = "SELECT data.symbol, symbols.name, "+

      "data.last, data.open, data.change, data.changeproc, "+

      "data.volume FROM DATA INNER JOIN SYMBOLS "+

      "ON DATA.symbol = SYMBOLS.symbol WHERE "+

      "month(data.date1)="+month+" AND day(data.date1)="+day+

      " AND year(data.date1)="+year;

    Thread runner = new Thread() {

      public void run() {

        try {

          // Load the JDBC-ODBC bridge driver

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

          Connection conn = DriverManager.getConnection(

            "jdbc:odbc:Market", "admin", "");

          Statement stmt = conn.createStatement();

          ResultSet results = stmt.executeQuery(query);

          boolean hasData = false;

          while (results.next()) {

            if (!hasData) {

              m_vector.removeAllElements();

              hasData = true;

            }

            String  symbol = results.getString(1);

            String  name = results.getString(2);

            double  last = results.getDouble(3);

            double  open = results.getDouble(4);

            double  change = results.getDouble(5);

            double  changePr = results.getDouble(6);

            long volume = results.getLong(7);

            m_vector.addElement(new StockData(symbol, name, last,

              open, change, changePr, volume));

          }

          results.close();

          stmt.close();

          conn.close();

          if (!hasData)    // We've got nothing

            m_result = 1;

        }

        catch (Exception e) {

          e.printStackTrace();

          System.err.println("Load data error: "+e.toString());

          m_result = -1;

        }

        m_date = date;

        Collections.sort(m_vector,

          new StockComparator(m_sortCol, m_sortAsc));

        m_result = 0;

      }

    };

    runner.start();

    return m_result;

  }

  // Unchanged code from section 18.4

}

// Class StockComparator unchanged from section 18.4

Understanding the Code

Class StocksTable

A JMenuBar instance is created with our custom createMenuBar() method and added to our frame.

The createMenuBar() method creates a menu bar containing a single menu titled "File." Two menu items are added: "Retrieve Data..." and "Exit" with a separator in between. Anonymous ActionListeners are added to each. The first calls our custom retrieveData() method, and the second simply kills the application using System.exit(0).

The retrieveData() method is called in response to a "Retrieve Data..." menu item activation. First it prompts the user to enter the date by displaying a JOptionPane dialog. Once the date has been entered, this method parses it using a SimpleDateFormat object. If the entered string cannot be parsed into a valid date, the method shows a warning message and returns. Otherwise, we connect to JDBC and retrieve new data. To indicate that the program will be busy for some time the wait mouse cursor is displayed. The main job is performed by our new StockTableData retrieveData() method (see below), which is invoked on the m_data object. StockTableData's retrieveData() method returns an error code that the rest of this method depends on:

0: Normal finish, some data retrieved. The table model is updated and repainted.

1: Normal finish, no data retrieved. A warning message is displayed, and no changes in the table model are made.

-1: An error has occurred. An error message is displayed, and no changes in the table model are made.

Class StockTableData

First, a minor change is required in the declaration of the m_date variable. Since now we've imported the java.sql package, which also includes the Date class, we have to do provide the fully qualified calls name java.util.Date.

A new instance variable is added to store the result of a data retrieval request in the retrieveData() method. As mentioned above, retrieveData() retrieves a table's data for a given date of trade. Our implementation uses the JDBC bridge driver and should be familiar to JDBC-aware readers. The first thing we do is construct an SQL statement. Since we cannot compare a java.util.Date object and an SQL date stored in the database, we have to extract the date's components (year, month, and day) and compare them separately. An instance of GregorianCalendar is used to manipulate the date object.

We load the JDBC-ODBC bridge driver to Microsoft Access by using the Class.forName method, and then connect to a database with the DriverManager.getConnection() method. If no exception is thrown, we can create a Statement instance for the newly created Connection object and retrieve a ResultSet by executing the previously constructed query.

While new data is available (checked with the ResultSet.next() method), we retrieve new data using basic getXX() methods, create a new StockData instance to encapsulate the new data, and add it to m_vector.

Note how the hasData local variable is used to distinguish the case in which we do not have any data in our RecordSet. The first time we receive some valid data from our RecordSet in the while loop, we set this variable to true and clean up our m_vector collection. If no data is found, we have an unchanged initial vector and the hasData flag is set to false. Finally we close our ResultSet, Statement, and Connection instances. If any exception occurs, the method prints the exception trace and returns a -1 to indicate an error. Otherwise our newly retrieved data is sorted with the Collections.sort() method and a 0 is returned to indicate success.

Running the Code

Figure 18.6 shows StocksTable with data retrieved from a database. Try loading data for different dates in your database. A sample Microsoft Access database, market.mdb, containing some real market data, can be found in the \swing\Chapter18 directory.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy