Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confusion about JTable / TableModel / Vectors and ResultSets

3 views
Skip to first unread message

KramerO

unread,
Jan 23, 2001, 3:29:13 PM1/23/01
to
Please Help!

How do I have to code java to get all my queried-data to a
vector?
Background: I need Vector-Objects to parse my datasets to the
TableModel for JTable View, don't I?

Please help me to understand concepts when talking about
TableModel and(!) ResultSets (Vectors).

Thanks a lot.

Oliver

akmal b chaudhri

unread,
Jan 23, 2001, 6:17:10 PM1/23/01
to

Oliver:

If you are using Sun's JDK, there should be a JTable example in a
sub-directory where the jdk is installed. I'm sorry I cannot give you the
exact sub-directory name, as I'm on a different machine to where I use
Java. Just recently I got it working with Cloudscape.

akmal

--
[ ---- akmal at soi.city.ac.uk ---- ]
[ http://www.soi.city.ac.uk/~akmal/ ]

Joe Cosby

unread,
Jan 23, 2001, 7:06:49 PM1/23/01
to
akmal b chaudhri <ak...@soi.city.ac.uk.nospam> hunched over a
computer, typing feverishly;
thunder crashed, akmal b chaudhri <ak...@soi.city.ac.uk.nospam>
laughed madly, then wrote:

>On Tue, 23 Jan 2001, KramerO wrote:
>
>> Please Help!
>>
>> How do I have to code java to get all my queried-data to a
>> vector?
>> Background: I need Vector-Objects to parse my datasets to the
>> TableModel for JTable View, don't I?
>>
>> Please help me to understand concepts when talking about
>> TableModel and(!) ResultSets (Vectors).
>>

This isn't really a very good example in a lot of ways, but if you
print this and look it over it might get you the right idea of going
from the column names/results vectors to a JTable. 'rs' is a SQL
query result set. It's not a great coding example. Ignore the
try/catch construct there. My evil twin brother did that.


java.sql.ResultSetMetaData md =
rs.getMetaData();
nCol = md.getColumnCount();
columnNames = new Vector();
rSRows = new Vector();

for(int i=1;i<=nCol;i++)
{
buffString = new
String(md.getColumnName(i));
columnNames.addElement(buffString);
}
while(rs.next())
{
Vector oneRow = new Vector();
for(int i=1;i<=NCol;i++)
{
buffString = rs.getString(i);
try
{
buffString.hashCode();
}
catch(Exception e)
{
buffString = "";
}
oneRow.addElement(BuffString);
} // end for(i)
rSRows.addElement(oneRow);
} // end while rs.next()

table = new JTable(rSRows,columnNames);
--
Joe Cosby
http://joecosby.home.mindspring.com

Man made booze, God made poison ivy -- WHO DO YOU TRUST?


Sig by Kookie Jar 5.98d http://go.to/generalfrenetics/

Loh

unread,
Jan 26, 2001, 4:28:16 PM1/26/01
to
Kramer;

If you do find the answer, please do let me know as I face the same
prob. Thanks

Loh
____________________________________________________ my message is:
I need some help and quick. Can someone please help me out. Maybe I need
another person's persective.

I need to retrieve data from my database and show it in a JTable. However
the error reply is always "Column not found" as pasted below. All the field
names are correct. Has it any thing to do with the column names or vector or
not the right data type. I should think the data type is correct. Can
someone just point to me the mistake. I have this project due very soon and
I have no where to turn to. Please take a look. Thanks.
----------------------------------------------
ERROR MESSAGE:

c:\jdk1.3\bin\java.exe QueryDatabase
Working Directory - C:\jdk1.3\Loh\new\
Class Path -
.;c:\Kawa4.01\kawaclasses.zip;c:\jdk1.3\lib\tools.jar;c:\jdk1.3\jre\lib\rt.j
ar;c:\jdk1.3\jre\lib\i18n.jar
Sat 27 January 2001 4:55

java.sql.SQLException: Column not found
at sun.jdbc.odbc.JdbcOdbcResultSet.findColumn(JdbcOdbcResultSet.java:1779)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:566)
at QueryDatabase.displayResultSet(QueryDatabase.java:167)
at QueryDatabase.getTable(QueryDatabase.java:114)
at QueryDatabase.<init>(QueryDatabase.java:45)
at QueryDatabase.main(QueryDatabase.java:241)
Process Exit...
____________________________
PROGRAM CODE

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class QueryDatabase extends JFrame {
private Connection connection;
private JTable table;
public Vector tableData;
public Vector columnHeads;

public QueryDatabase()

// The URL specifying the Stock database to which
// this program connects using JDBC to connect to a
// Microsoft ODBC database.
String url = "jdbc:odbc:Stock";
String username = "anonymous";
String password = "guest";

// Load the driver to allow connection to the database
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

connection =
verManager.getConnection(
url, username, password );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
}

getTable();
setSize( 450, 150 );
show();
}

private final static String[] WeekDays =
{"Sun", "Mon", "Tue", "Wed", "Thurs", "Fri", "Sat"};

private final static String[] Months =
{"January", "Febuary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};

private static String dateToString(java.util.Date e)
{
String buf = WeekDays[e.getDay()] + " " +
e.getDate() + " " +
Months[e.getMonth()] + " " +
(e.getYear() + 1900) + " " +
e.getHours() + ":" +
e.getMinutes();
return(buf);
}

private static int hoursToInt(java.util.Date r)
{
int buf = r.getHours();
return(buf);
}

private static int minutesToInt(java.util.Date f)
{
int buf = f.getMinutes();
return(buf);
}

private void getTable()
{
Statement statement;
ResultSet resultSet;
try {

java.util.Date e = new java.util.Date();
System.out.println(dateToString(e));

java.util.Date r = new java.util.Date();
int myHour = hoursToInt(r);

java.util.Date f = new java.util.Date();
int myMinutes = minutesToInt(f);
int temp = myMinutes / 5;
myMinutes = temp * 5;

String query = "SELECT Name, Price, Volume, Negative, Positive " +
"FROM StockPrice WHERE Hour = " + myHour +

" AND Minute = " + myMinutes +
" ORDER BY Hour, Minute, Name ASC";

statement = connection.createStatement();
resultSet
= statement.executeQuery( query );
displayResultSet( resultSet );
System.out.println("Hi");
System.out.println(myMinutes);

statement.close();
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
}
}

private void displayResultSet( ResultSet rs)
throws SQLException
{
Statement statement;
ResultSet resultSet;
{
// position to first record
boolean moreRecords = rs.next();

// If there are no records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}
}

setTitle( "Stock Summary" );

Vector columnHeads = new Vector();
Vector tableData = new Vector();

try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );

// get row data
do {
int hr = rs.getInt("Hour");
int min = rs.getInt("Minute");

String nam = rs.getString("Name");
Integer namewrapper = new Integer( nam );

double pr = rs.getDouble("Price");
Double pricewrapper = new Double( pr );

int vol = rs.getInt("Volume");
Integer volwrapper = new Integer( vol );

int neg = rs.getInt("Negative");
Integer negwrapper = new Integer( neg );

int pos = rs.getInt("Positive");
Integer poswrapper = new Integer( pos );

tableData.addElement(new MyRowData(namewrapper,
pricewrapper, volwrapper, negwrapper, poswrapper));

}

while (rs.next());

// display table with ResultSet contents
table = new JTable(columnHeads,tableData);
JScrollPane scroller = new JScrollPane( table );

ContentPane().add(
scroller, BorderLayout.CENTER );
validate();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}

public Vector getTableData()
{
return tableData;
}

public Vector getColumnHeads()
{
return this.columnHeads;
}

public void shutDown()
{
try {
connection.close();
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to disconnect" );
sqlex.printStackTrace();
}
}

file://---------------------------main----------------------------

public static void main( String args[] )
{
final QueryDatabase app = new QueryDatabase();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.shutDown();
System.exit( 0 );
}
}
);
}
}

..

0 new messages