CG
asked on
Display a result set in a TextArea
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)
{
super(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");
r1.addActionListenter(repo rtHandler) ;
report.add(r1);
ViewMenu.add(report);
setMenuBar(mbar);
t = new TextArea();
t.setEditable(false);
add("Center", t);
//{{REGISTER_LISTENERS
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
setVisible(false);}
});
}
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(" ");
System.out.print(rsmd.getC olumnLabel (i));
}
System.out.println("");
System.out.println("------ ---------- ---------- ---------- ");
//Display data, fetching until end of the result set
boolean more = rs.next();
while(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(rs.getStr ing(i));
}
System.out.print(" ");
// Fetch the next result set row
more = rs.next();
}
}
class ReportHandler implements ActionListener {
public void actionPerformed(ActionEven t event){
String command = event.getActionCommand();
if(command.equals("Student s")){
try{
showResultSet(q.doQueryR1( ));
}catch(SQLException e){
System.out.println(e.getMe ssage());
}
}
}
}
}
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)
{
super(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");
r1.addActionListenter(repo
report.add(r1);
ViewMenu.add(report);
setMenuBar(mbar);
t = new TextArea();
t.setEditable(false);
add("Center", t);
//{{REGISTER_LISTENERS
this.addWindowListener(new
public void windowClosing(WindowEvent e){
dispose();
setVisible(false);}
});
}
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(" ");
System.out.print(rsmd.getC
}
System.out.println("");
System.out.println("------
//Display data, fetching until end of the result set
boolean more = rs.next();
while(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(rs.getStr
}
System.out.print(" ");
// Fetch the next result set row
more = rs.next();
}
}
class ReportHandler implements ActionListener {
public void actionPerformed(ActionEven
String command = event.getActionCommand();
if(command.equals("Student
try{
showResultSet(q.doQueryR1(
}catch(SQLException e){
System.out.println(e.getMe
}
}
}
}
}
ASKER
Edited text of question
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().
Cheers.
-------------------------- -
protected void showResultSet(ResultSet r)
{
try
{
ResultSetMetaData md = r.getMetaData();
String ctitle, cval;
int ctype = 0, csize, ccount;
boolean first = true;
String displayrow = "";
ccount = md.getColumnCount();
while (r.next())
{
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(displayrow);
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";
}
else
{
if (ctype == Types.OTHER)
{
cval = "OBJ";
}
else
{
cval = r.getString(i);
if (r.wasNull())
cval = "NULL";
}
}
displayrow += allignString(cval, ' ', csize);
}
writeln(displayrow);
}
}
catch (SQLException e)
{
while (e != null)
{
writeln("[error] " + e.getMessage());
e = e.getNextException();
sqlerrors++;
}
}
}
/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);
}
else
{
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);
}
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().
Cheers.
--------------------------
protected void showResultSet(ResultSet r)
{
try
{
ResultSetMetaData md = r.getMetaData();
String ctitle, cval;
int ctype = 0, csize, ccount;
boolean first = true;
String displayrow = "";
ccount = md.getColumnCount();
while (r.next())
{
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(displayrow);
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";
}
else
{
if (ctype == Types.OTHER)
{
cval = "OBJ";
}
else
{
cval = r.getString(i);
if (r.wasNull())
cval = "NULL";
}
}
displayrow += allignString(cval, ' ', csize);
}
writeln(displayrow);
}
}
catch (SQLException e)
{
while (e != null)
{
writeln("[error] " + e.getMessage());
e = e.getNextException();
sqlerrors++;
}
}
}
/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);
}
else
{
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);
}
ASKER
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.
me how to call the function showResultSet() when the user selects the subMenuItem
in the menu bar.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(q.rs.getT ime(i));
System.out.println(cval);
}
but it does not work. The system did not print out anythiing.
2. How to set the font of the title string " Font.PLAIN"
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(q.rs.getT
System.out.println(cval);
}
but it does not work. The system did not print out anythiing.
2. How to set the font of the title string " Font.PLAIN"
ASKER
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
ASKER