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

Error while displaying Data

0 views
Skip to first unread message

stevenq

unread,
Jan 30, 2001, 1:30:37 PM1/30/01
to
hi all
i'm trying to display a database in Microsoft Access, but there
no display and prompt me the following message, Exception
raised:"java.lang.ArrayIndexOutOfBoundsException: 1>=1"
pls help thankx

Joe Cosby

unread,
Jan 30, 2001, 3:10:15 PM1/30/01
to
stevenq <stevenq...@sun.partner.remarq.com.invalid> hunched over
a computer, typing feverishly;
thunder crashed, stevenq
<stevenq...@sun.partner.remarq.com.invalid> laughed madly, then
wrote:

You'll need to isolate where in your code the error is occuring.

The error you're getting isn't specific to the database connection,
it's somewhere in your code. It sounds like you have an array with a
dimension of 1, which was probably created incorrectly.
--
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/

stevenq

unread,
Feb 2, 2001, 4:16:34 PM2/2/01
to
had been trying these couple of days still can't get it solve.
here's my code:

import java.util.*;
import java.sql.*;
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TableDisplay extends JFrame {
private Connection connection;
private JTable table;

public TableDisplay()
{
//{{INIT_CONTROLS
//}}

//The URL specifying the Readings database to which this
program
//connects using JDBC to connect to a Microsoft ODBC
database (Access)
String URL = "jdbc:odbc:Readings";
String Login_name = "STEVEN";
String Password = "fyp";

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

connection = DriverManager.getConnection(
URL , "STEVEN" , "fyp" );

}
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();
System.exit(1); //terminate program
}

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

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

try {
String query = "SELECT * FROM Measurement";

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 more records, display a message
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}

setTitle( "Date from the Generator" );

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 );
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.DATE: currentRow.addElement ( rs.getDate(
i ) );
break;
case Types.TIME: currentRow.addElement ( rs.getTime(
i ) );
break;
case Types.INTEGER: currentRow.addElement( new Double
(rs.getDouble ( 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 );
}
}
);
}
}
It's really urgent, would someone out there pls help!!!
thanks


Joe Cosby

unread,
Feb 4, 2001, 5:53:38 PM2/4/01
to
stevenq <stevenq...@sun.partner.remarq.com.invalid> hunched over
a computer, typing feverishly;
thunder crashed, stevenq
<stevenq...@sun.partner.remarq.com.invalid> laughed madly, then
wrote:

>had been trying these couple of days still can't get it solve.
>here's my code:
>

OK, a couple things here.

When you post a question like this, in order for anybody to help you,
they need to know a little more specifically what is going on.

Otherwise, the only way anybody can help you is to do the job of
debugging your code themselves on your behalf.

It's reasonable to come to a forum hoping to get the insight of people
with more experience, but it isn't reasonable to expect them to do
work that you could do as easily as them.

What you need to do is -isolate- the problem in your code.

I compiled this and ran it just now, but usually people aren't going
to do that much work.

The most important thing, first of all, is that you learn to look at
the output of your program. When your program crashed, a message
block like this:

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:417)
at javax.swing.JTable$1.getValueAt(JTable.java:415)
at javax.swing.JTable.getValueAt(JTable.java:1711)
at javax.swing.JTable.prepareRenderer(JTable.java:3530)
at
javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:995)
at
javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:917)
at
[snip]

appeared in your console window. Usually, the first line of the
message will tell you which line of code in your program was being
executed when it failed. That way, when you ask for help, you know
precisely where the error occurred.

If you had been watching your console window, you would have also
noticed a long series of messages saying 'Type was: VARCHAR', if you
have any VARCHAR field types in your data. If you look at the switch
statement around line 112 in your program:

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch (rsmd.getColumnType( i ) ) {
case Types.DATE: currentRow.addElement ( rs.getDate(
i ) );
break;
case Types.TIME: currentRow.addElement ( rs.getTime(
i ) );
break;
case Types.INTEGER: currentRow.addElement( new Double (rs.getDouble (
i ) ) );
break;
default: System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}

If that message is printed, your switch statement doesn't add that
element to the vector. Besides that, it doesn't look to me like your
'for' loop does anything. I could be wrong. It isn't followed by a
conventional block; since it has no semicolon at the end I -think- it
will execute the next line (the switch statement), whether it will
execute the whole switch statement or not I honestly don't know.

Another thing, you're pre-incrementing 'i'. By putting the two plus
signs to the left rather than the right, what you're doing is:

store a value of 1 to i
add one to i
execute the code in the loop

So the first time you go through the loop, i=2.

Use conventional style. Follow a for statement with a clearly marked
block. I suppose I should be able to answer the question of whether
the switch will execute correctly or not if I ever hope to take the
Java certification, but for daily work you shouldn't have to ask that
kind of question. Do it right. Mark your blocks.

Anyway, that switch statement needs to get fixed. I'm not sure if
that's all of the problem, but it's where I would go next if this were
my program.

Please, learn the basic concepts of debugging if you expect help in
the future, and the basic concepts of style.

"Brain Death For Bonzo," in radiant Alz-o-Vision!!!
- Hellpope Huey

0 new messages