Vectors: How to add a date object to them

Hello Everyone. I'm trying to build a JTable from 2 Vectors (e.g. MyTable(Vector(column names), Vector (row date)) that will contain values obtained from a query through JDBC-ODBC bridge from an Access db. I have a book that shows an example on how to do it, but it doesn't show what to do when an Access field has date values. PLEASE help me. Here's the code:

import java.sql.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class TableDisplay extends JFrame {

private Connection connection;

private JTable table;

public TableDisplay()

{

// The URL specifying the Books database to which

// this program connects using JDBC to connect to a

// Microsoft ODBC database.

String url = "jdbc:odbc:Northwind";

String username = "anonymous";

String password = "guest";

// Load the driver to allow connection to the database

try {

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

connection = DriverManager.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 void getTable()

{

Statement statement;

ResultSet resultSet;

try {

String query = "SELECT * FROM Orders";

statement = connection.createStatement();

resultSet = statement.executeQuery( query );

displayResultSet( resultSet );

statement.close();

}

catch ( SQLException sqlex ) {

sqlex.printStackTrace();

}

}

private void displayResultSet( ResultSet rs )

throws SQLException

{

// 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( "Authors table from Books" );

// These Vectors will be used to build a JTable

Vector columnHeads = new Vector();

Vector rows = 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 {

rows.addElement( getNextRow( rs, rsmd ) );

} while ( rs.next() );

// display table with ResultSet contents

table = new JTable( rows, columnHeads );

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane scroller = new JScrollPane( table );

getContentPane().add(

scroller, BorderLayout.CENTER );

validate();

}

catch ( SQLException sqlex ) {

sqlex.printStackTrace();

}

}

private Vector getNextRow( ResultSet rs,

ResultSetMetaData rsmd )

throws SQLException

{

Vector currentRow = new Vector();

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

switch( rsmd.getColumnType( i ) ) {

case Types.CHAR:

case Types.VARCHAR:

case Types.LONGVARCHAR:

currentRow.addElement( rs.getString( i ) );

break;

case Types.TINYINT:

case Types.SMALLINT:

case Types.INTEGER:

currentRow.addElement(

new Long ( rs.getLong( i ) ) );

break;

case Types.BIGINT:

currentRow.addElement(

new Long ( rs.getLong( i ) ) );

break;

case Types.FLOAT:

case Types.DOUBLE:

currentRow.addElement(

new Double( rs.getDouble( i ) ) );

break;

// !!!!!! This is where I need help at

case Types.DATE:

currentRow.addElement(rs.getDate(i));

break;

default:

System.out.println( "Type was: " +

rsmd.getColumnTypeName( i ) );

}

return currentRow;

}

public void shutDown()

{

try {

connection.close();

}

catch ( SQLException sqlex ) {

System.err.println( "Unable to disconnect" );

sqlex.printStackTrace();

}

}

public static void main( String args[] )

{

final TableDisplay app = new TableDisplay();

app.addWindowListener(

new WindowAdapter() {

public void windowClosing( WindowEvent e )

{

app.shutDown();

System.exit( 0 );

}

}

);

}

}

[4848 byte] By [ham17151] at [2007-9-26 3:11:50]
# 1

If you are just going to display this to the local user, you don't have to go to all that trouble. Just use ResultSet.getObject() and place the value directly into the Vector. JTable just calls the toString() function on the objects you put in the vector for display purposes. If you need to serialize these objects then you can convert the output from getObject() with a method like the following:

public static Object getCorrectObject(

Objectvalue ) {

if (value != null) {

if (value.getClass().isAssignableFrom(String.class)) {

return value ;

} else if (value.getClass().isAssignableFrom(java.util.Date.class)) {

return new java.util.Date(((java.util.Date)value).getTime()) ;

} else if (value.getClass().isAssignableFrom(Number.class)) {

Number number = (Number)value ;

if (number.floatValue() != Math.abs(number.floatValue())) {

//Number has a decimal

return new Float(number.floatValue()) ;

} else if ((new Integer(number.intValue())).longValue() != number.longValue()) {

//Number is big enough to be a Long

return new Long(number.longValue()) ;

} else {

//Number can be an Integer

return new Integer(number.intValue()) ;

} //end if

} //end if

} //end if

return null ;

} //end getCorrectObject()

Complication at 2007-6-29 11:19:42 > top of Java-index,Archived Forums,New To Java Technology Archive...