Display a result set in a TextArea

Posted on 1998-10-11
Last Modified: 2012-08-13
Can anybody help me to correct and improve the following program? I use JDK1.1.6.
I want to display all columns and rows of the given result set in
a textArea when the user selects the item "View"->"Report"->
"Students". Also, each report should have a Title and
date. For example:
                   Student's Report
Oct. 10, 1998

FirstName       LastName      Course       Grade

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class ReportsFrame extends Frame implements ActionListener
   static TextArea t;
   DataQuery q;
   ReportHandler reportHandler;

   public ReportsFrame(String s)
       setTitle( "Reports" );
       setSize(700, 450);
       MenuBar mbar = new MenuBar();
       Menu ViewMenu = new Menu( "View" );
       Menu report = new Menu( "Report" );  
       MenuItem r1 = new MenuItem("Students");

       t = new TextArea();
       add("Center", t);
       this.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){

    protected void showResultSet(ResultSet rs) throws SQLException{
       int i;
       //Get the ResultSetMetaData. This will be used for
       //the column headings

       ResultSetMetaData rsmd = rs.getMetaData();
       //Get the number of columns in the result set
       int numCols = rsmd.getColumnCount();
       //Display column headings
       for( i = 1; i<=numCols; i++){
           if(i> 1) System.out.print("  ");
       //Display data, fetching until end of the result set

       boolean more =;
           //Loop through each column, getting the column
           //data and displaying
           for (i=1; i<=numCols; i++){
               if(i>1)   System.out.print("                ");
           System.out.print("                ");
           // Fetch the next result set row
           more =;

   class ReportHandler implements ActionListener {
       public void actionPerformed(ActionEvent event){
            String command = event.getActionCommand();
                }catch(SQLException e){
Question by:CG
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2

Author Comment

ID: 1225657
Edited text of question

Author Comment

ID: 1225658
Edited text of question

Expert Comment

ID: 1225659
Here is how I do it. I have two help methods, fillString() and alignString(). You can use them to format a string to a specific length. The text area does not allow writing on a specific position (row, column), but you can generate the whole rows and append them as text. the scenarion is:
1. Set a fixed width font for the TextArea.
2. For each row of the result set, use the information from the MetaData to get its width and prepare String row for appending to the TextArea using correct calls to alignString() and fillString().


       protected void showResultSet(ResultSet r)
           ResultSetMetaData md = r.getMetaData();
           String ctitle, cval;
           int ctype = 0, csize, ccount;
           boolean first = true;
           String displayrow = "";

           ccount = md.getColumnCount();

           while (
             if (first)
               first = false;
               displayrow = "";
               for (int i = 1; i <= ccount; i++)
                 csize = md.getColumnDisplaySize(i);
                 ctitle = md.getColumnLabel(i);
                 displayrow += allignString(ctitle, ' ', csize);
               writeln(fillString('-', displayrow.length()));
             displayrow = "";
             for (int i = 1; i <= ccount; i++)
               ctype = md.getColumnType(i);
               csize = md.getColumnDisplaySize(i);
               if ((ctype == Types.BINARY) || (ctype == Types.LONGVARBINARY) || (ctype == Types.VARBINARY))
                 cval = "BIN";
                 if (ctype == Types.OTHER)
                   cval = "OBJ";
                   cval = r.getString(i);
                   if (r.wasNull())
       cval = "NULL";
               displayrow += allignString(cval, ' ', csize);
         catch (SQLException e)
           while (e != null)
             writeln("[error] " + e.getMessage());
             e = e.getNextException();

/this one trims a string to a size or fills it with character
       //to this size. It is usefull to make strings specifically long
       protected String allignString(String s, char c, int size)
         String res = "";
         int len = s.length();
         if (len >= size)
           res = s.substring(0, size);
           String ccc = fillString(c, size - len);
           res = s + ccc;
         return res;
       //this one produces a string with length sizefilled with char c
       protected String fillString(char c, int size)
         char data[] = new char[size];
         for (int i = 0; i < size; i++)
          data[i] = c;
         return new String(data);
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1225660
I don't know how to correct my program upon your suggest. Also, you did not tell
me how to call the function  showResultSet() when the user selects the subMenuItem
in the menu bar.

Accepted Solution

diakov earned 100 total points
ID: 1225661
You have to set the font of the text area to:

t.setFont(new Font("Courier", Font.PLAIN, 12));
//then the alignment works, because each character will use the same space.
// If we assume that we know the max number of chars per line,
//you can print the title
int max_num_chars_per_line = 100;
String title = "Students"
String title_row = fillString(' ', (100 - title.length())/2) + title;

//You can print the date you wanted by
Date dt = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("MMM. dd, yyyy");
String date_row = sdf.format(dt);

Hope this helps,



Author Comment

ID: 1225662
Hi, Diakov:
Thank you for your help!  Could you please  help me to solve two more problems (40 points)?
1.  When the program gets data with type "Date/Time" using getString() from the database, it will show such as "1899-12-30 08:00:00".
So I try to add code:
  if (ctype == Types.TIME)
                   cval = formatter.format(;
but it does not work.  The system did not print out anythiing.

2. How to set the font of the title string " Font.PLAIN"

Author Comment

ID: 1225663
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a");


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 48
collection output issue 9 61
import as existing maven project 3 37
Given a github repo find email address of all collaborators in that repo 1 32
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question