For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2006 > Retrieving information from MySQL Database and Placing in JTable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Retrieving information from MySQL Database and Placing in JTable
christopher_board@yahoo.co.uk

2006-09-23, 7:02 pm

Hi. I am writing a Java Application that send and receives information
from the MySQL Database. I am trying to receive information from the
MySQL Database and place it in a JTable in Java. It is getting the
information from the database as it is outputting to the system how
many records there are in the database but it is only placing the last
record in the database into the table. Below is the code that I am
using to do this:

public void showDatabase() {
enterDatabase();
try {
System.out.println("Showing database");
Statement s = connection.createStatement();
s.executeQuery("SELECT * FROM " +

pocket_money_calculator.Login.txtUser.getText() + "");
ResultSet rs = s.getResultSet();
rs.last();
int mycount = rs.getRow();
System.out.println("Number of records before retrieval is"
+
mycount);
rs.beforeFirst();
while (rs.next()) {
String date = rs.getString("date");
int spent = rs.getInt("spent");
int received = rs.getInt("received");
String who = rs.getString("who");
String shop = rs.getString("shop");
String bought = rs.getString("bought");
String reason = rs.getString("reason");
int balance = received - spent;
String[] Cols = {"Date", "Spent", "Received", "Shop",
"Who",
"Reason",
"Bought", "Total Balance"};
String Data[][] = { {"" + date + "", "" + spent + "",
"" + received + "",
"" + shop + "", "" + who + "",
"" + reason + "",
"" + bought + "", "" + balance + ""}
};
JTable tblResults = new JTable(Data, Cols);
tblResultsPane.add(tblResults);
tblResultsPane.getViewport().add(tblResults);
System.out.println("" + balance);
}
} catch (SQLException ex) {
System.err.println("" + ex.toString());
}
}

Any help would be apreciated in this matter.

Thanks

Andrew Thompson

2006-09-23, 10:02 pm

christopher_board@yahoo.co.uk wrote:

> while (rs.next()) {

....
> JTable tblResults = new JTable(Data, Cols);


It does seem you are making as many JTables as
the result set has records..

Andrew T.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com