|
-
April 21st, 2008, 07:42 PM
#1
Questions about ResultSetMetaData
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 dbc: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);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|