Retrieving information from MySQL Database and Placing in JTable
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