[unchecked] unchecked error message

I wanted to display data in a jtable and decided to use code from another forum. When I compile the code I get three warning messages:

"[unchecked] unchecked call to addElement(E)...."

These warning messages are for the lines of code that call the addElement method.

Here is the code:

import java.awt.*;

import java.io.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

public class TableFromDatabase extends JFrame

{

public TableFromDatabase()

{

Vector columnNames = new Vector();

Vector data = new Vector();

try

{

// Connect to the Database

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

String url = "jdbc:odbc:moviesDSN"; // if using ODBC Data Source name

//String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/teenergy.mdb";

String userid = "";

String password = "";

Class.forName( driver );

Connection connection = DriverManager.getConnection(url);

// Read data from a table

String sql = "Select * from Employees2";

Statement stmt = connection.createStatement();

ResultSet rs = stmt.executeQuery( sql );

ResultSetMetaData md = rs.getMetaData();

int columns = md.getColumnCount();

// Get column names

for (int i = 1; i <= columns; i++)

{

columnNames.addElement( md.getColumnName(i) );

}

// Get row data

while (rs.next())

{

Vector row = new Vector(columns);

for (int i = 1; i <= columns; i++)

{

row.addElement( rs.getObject(i) );

}

data.addElement( row );

}

rs.close();

stmt.close();

}

catch(Exception e)

{

System.out.println( e );

}

// Create table with database data

JTable table = new JTable(data, columnNames);

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

JPanel buttonPanel = new JPanel();

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

}

@SuppressWarnings("unchecked") public static void main(String[] args)

{

TableFromDatabase frame = new TableFromDatabase();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

[2396 byte] By [cmartin19a] at [2007-11-27 2:04:59]
# 1

This has completely nothing to do with JDBC, but with the (lack of) use of Generics.

Since Generics were introduced in Java 1.5 in the summer of 2004, you have to parameterize the collections.

Here is a simple and clean tut: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

By the way, don't forget to close the connection.

BalusCa at 2007-7-12 1:49:44 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...