Click to See Complete Forum and Search --> : Questions about ResultSetMetaData


a java student
April 21st, 2008, 07:42 PM
When I compiled these codes, I got a compiler error indicating that it uses unchecked or unsafe operations. Can anyone tell me what I did wrong with the ResultSetMetaData? Thanks
===================================================


public void ProcessSQL()
{
//use jdbc-odbc to connect to MS Access BOOKS.mdb
String url = "jdbc:odbc:BOOKS";
String userID = "anonymous";
String password = "guest";

//load driver to allow connection to the database
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url, userID,
password);

statement = connection.createStatement();
resultset = statement.executeQuery(ta.getText());

//display results
moreRecords = resultset.next();

if (!moreRecords)
JOptionPane.showMessageDialog(this, "No Records",
"Result Set", JOptionPane.INFORMATION_MESSAGE);

//get column headings
rsmd = resultset.getMetaData();
int count = rsmd.getColumnCount();
for (int i = 1; i <= count; ++i)
columns.addElement(rsmd.getColumnName(i));

//get row data
while (resultset.next())
{
//get a row
Vector currentRow = new Vector();
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
{
switch(rsmd.getColumnType(i))
{
case Types.VARCHAR:
currentRow.addElement(resultset.getString(i));
break;

case Types.INTEGER:
currentRow.addElement(resultset.getLong(i));
break;

default:
JOptionPane.showMessageDialog(this, "Type is
" + rsmd.getColumnTypeName(i), "Warning",
JOptionPane.INFORMATION_MESSAGE);

} //end of switch

} //end of for

rows.addElement(currentRow);

} // end of while

jt = new JTable(rows, columns);

statement.close(); //close the db
connection.close();
}

catch (ClassNotFoundException cnfe)
{
JOptionPane.showMessageDialog(this, cnfe.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1); //terminate
}
catch (SQLException sqle)
{
JOptionPane.showMessageDialog(this, sqle.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}

dlorde
April 22nd, 2008, 04:34 AM
Are you sure it was an error, not a warning? If you are using Java 5 or above, you will get unchecked or unsafe operation warnings if you use a generic collection without specifying a type (i.e. use it in the traditional way, without generics). These warnings are harmless, and simply indicate that your arguments won't be type-checked with generics.

If you want a more definite answer, post up the full error message text + stack trace, and put [CODE]...[/CODE] tags around your formatted code so it is readable.

Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson & G. Sussman

a java student
April 22nd, 2008, 10:22 AM
Hi dlorde,

I think it was a warning. When I compiled with javac PracticeJDBC.java, I got this warning messages "Note: PracticeJDBC.java uses unchecked or unsafeoperations. Note: Recompile with -Xlint: unchecked for details."
What should I do when I get this type of messages?

Can you please explain to me what do you mean by "using it in traditional way without generics" as you know I am learning Java? Can you please show me how to correct this problem ?

Thank again.

dlorde
April 22nd, 2008, 05:27 PM
What should I do when I get this type of messages?You can safely ignore this warning until you get to grips with generics. I recommend that you get comfortable with the basics first, but it's up to you.

Can you please explain to me what do you mean by "using it in traditional way without generics"Up to Java version 5, all the collection classes in the SDK stored everything as Objects. This meant that when retrieving an object from the collection, you would have to cast it from Object back to the actual class type. This is downcasting, casting down to a subclass from a superclass. This is what I called the 'traditional' way. The problem is that you have to get it right or you'll get a potentially fatal exception at runtime - the compiler doesn't know what the real class of the object is, so it can't warn you of possible mistakes in casting.

Since Java 5, generics have been added to the language. These allow you to specify object type you're going to use in a collection. The SDK collection classes now support this feature, and if you use it, you don't have to downcast any more, and the compiler will tell you if you try to retrieve the wrong object type. The generics feature is backwardly compatible, so code that doesn't use it will still compile - but the compiler will warn you that it can't check your use of that collection (casting, etc).

as you know I am learning Java? Can you please show me how to correct this problem ?You could specify the object type your Vector is going to contain (it's generally recommended that you use ArrayList reather than Vector, unless you have multi-threaded code):// Declare a Vector containing only Strings:
Vector<String> currentRow = new Vector<String>();
currentRow.addItem("An Item"); //add a String
String item = currentRow.get(0); // get the String back, with no casting
However, I notice your code puts both Strings and Longs into your Vector, which is not a good idea. It's dangerous because you have to know the real type of every item in there to use it safely, and it also means you can't use generics because you can't specify two different types for a single collection object. If you need to store a number of different types together, it's probably best to declare a class that holds them as member variables (fields).

By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race...
A.N. Whitehead

a java student
April 22nd, 2008, 06:28 PM
Hi dlorde, Thank you very much for your time.