For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2006 > Retrieving information from MySQL Database









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
christopher_board@yahoo.co.uk

2006-09-25, 10:02 pm

Hi. I am trying to retrieve information from a MySQL database and
placing it in a JTable. However the code I have used gets the results
and displays it in the system output. However no results are placed in
the JTable. Does anyone know the reason. Below is the code:

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

pocket_money_calculator.Login.txtUser.getText() +
";";
ResultSet table = statement.executeQuery(query);

// determine properties of table
ResultSetMetaData meta = table.getMetaData();
String[] colNames = new String[meta.getColumnCount()];
Vector[] cells = new Vector[colNames.length];
for (int col = 0; col < colNames.length; col++) {
colNames[col] = meta.getColumnName(col + 1);
cells[col] = new Vector();
}

// hold data from result set
while (table.next()) {
for (int col = 0; col < colNames.length; col++) {
Object cell = table.getObject(colNames[col]);
cells[col].add(cell);
}
}

// print column headings
for (int col = 0; col < colNames.length; col++) {
System.out.print(colNames[col].toUpperCase() + "\t");
}
System.out.println();

// print data row-wise
while (!cells[0].isEmpty()) {
for (int col = 0; col < colNames.length; col++) {
System.out.print(cells[col].remove(0).toString()
+ "\t");
}
System.out.println();
JTable tblResults = new JTable();
tblResultsPane.add(tblResults);
tblResultsPane.getViewport().add(tblResults);
}

} catch (SQLException ex) {
System.err.println("" + ex.toString());
}
// exit more gently
catch (Exception e) {
e.printStackTrace();
}
}


Any help would be apreciated. Thanks very much for your help

Jeff

2006-09-25, 10:02 pm


christopher_board@yahoo.co.uk wrote:
> Hi. I am trying to retrieve information from a MySQL database and
> placing it in a JTable. However the code I have used gets the results
> and displays it in the system output. However no results are placed in
> the JTable. Does anyone know the reason. Below is the code:
>
> public void showDatabase() {
> try {
> System.out.println("Showing database");
> Statement s = connection.createStatement();
> String query = "SELECT * FROM " +
>
> pocket_money_calculator.Login.txtUser.getText() +
> ";";
> ResultSet table = statement.executeQuery(query);
>
> // determine properties of table
> ResultSetMetaData meta = table.getMetaData();
> String[] colNames = new String[meta.getColumnCount()];
> Vector[] cells = new Vector[colNames.length];
> for (int col = 0; col < colNames.length; col++) {
> colNames[col] = meta.getColumnName(col + 1);
> cells[col] = new Vector();
> }
>
> // hold data from result set
> while (table.next()) {
> for (int col = 0; col < colNames.length; col++) {
> Object cell = table.getObject(colNames[col]);
> cells[col].add(cell);
> }
> }
>
> // print column headings
> for (int col = 0; col < colNames.length; col++) {
> System.out.print(colNames[col].toUpperCase() + "\t");
> }
> System.out.println();
>
> // print data row-wise
> while (!cells[0].isEmpty()) {
> for (int col = 0; col < colNames.length; col++) {
> System.out.print(cells[col].remove(0).toString()
> + "\t");
> }
> System.out.println();
> JTable tblResults = new JTable();
> tblResultsPane.add(tblResults);
> tblResultsPane.getViewport().add(tblResults);
> }
>
> } catch (SQLException ex) {
> System.err.println("" + ex.toString());
> }
> // exit more gently
> catch (Exception e) {
> e.printStackTrace();
> }
> }
>
>
> Any help would be apreciated. Thanks very much for your help



I am also working on a java/MySQL project, and have a couple
suggestions (I am a relative newcomer to Java, not exactly a newbie,
but not experienced either, yet). First, there is an excellent book
titled MySQL and Java Developer's Guide by Mark Matthews, published by
Wiley. I find it helpful. The other is a bit of a cheat, and that is a
component I have found immensely useful and rather easy to use called
QuickTable. In essence it is a data-aware JTable. Displaying data in
the table is as simple as getting your result set then:
dBTable1.refresh(myResultSet);
but there is a large set of additional routines, allowing you to change
headers, print tables, etc.
hth
js

Ian Wilson

2006-09-26, 8:41 am

christopher_board@yahoo.co.uk wrote:
> Hi. I am trying to retrieve information from a MySQL database and
> placing it in a JTable. However the code I have used gets the results
> and displays it in the system output. However no results are placed in
> the JTable. Does anyone know the reason.


Yes, I do. You are not passing any data to your JTable constructor.

<code snipped>

You are also constructing the JTable inside a loop, this seems a bad idea.

Since your "cells" variable isn't suitable for passing to JTable()
directly, I'd use a TableModel to feed your "cells" data to a JTable. I
haven't looked closely at your code so I'm assuming your "cells"
variable eventually holds the entire set of data for the table.
thierry

2006-09-26, 7:03 pm

christopher_board@yahoo.co.uk wrote:
> Hi. I am trying to retrieve information from a MySQL database and
> placing it in a JTable. However the code I have used gets the results
> and displays it in the system output. However no results are placed in
> the JTable. Does anyone know the reason. Below is the code:
>
> public void showDatabase() {
> try {
> System.out.println("Showing database");
> Statement s = connection.createStatement();
> String query = "SELECT * FROM " +
>
> pocket_money_calculator.Login.txtUser.getText() +
> ";";
> ResultSet table = statement.executeQuery(query);
>
> // determine properties of table
> ResultSetMetaData meta = table.getMetaData();
> String[] colNames = new String[meta.getColumnCount()];
> Vector[] cells = new Vector[colNames.length];
> for (int col = 0; col < colNames.length; col++) {
> colNames[col] = meta.getColumnName(col + 1);
> cells[col] = new Vector();
> }
>
> // hold data from result set
> while (table.next()) {
> for (int col = 0; col < colNames.length; col++) {
> Object cell = table.getObject(colNames[col]);
> cells[col].add(cell);
> }
> }
>
> // print column headings
> for (int col = 0; col < colNames.length; col++) {
> System.out.print(colNames[col].toUpperCase() + "\t");
> }
> System.out.println();
>
> // print data row-wise
> while (!cells[0].isEmpty()) {
> for (int col = 0; col < colNames.length; col++) {
> System.out.print(cells[col].remove(0).toString()
> + "\t");
> }
> System.out.println();
> JTable tblResults = new JTable();
> tblResultsPane.add(tblResults);
> tblResultsPane.getViewport().add(tblResults);
> }
>
> } catch (SQLException ex) {
> System.err.println("" + ex.toString());
> }
> // exit more gently
> catch (Exception e) {
> e.printStackTrace();
> }
> }
>
>
> Any help would be apreciated. Thanks very much for your help
>

Hi, i'm not sure but tblResults is empty.
Sponsored Links







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

Copyright 2008 codecomments.com