CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2008
    Posts
    23

    Question 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 = "jdbcdbc: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);
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Questions about ResultSetMetaData

    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 &#91;CODE]...&#91;/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
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2008
    Posts
    23

    Question Re: Questions about ResultSetMetaData

    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.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Questions about ResultSetMetaData

    Quote Originally Posted by a java student
    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):
    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
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Mar 2008
    Posts
    23

    Smile Re: Questions about ResultSetMetaData

    Hi dlorde, Thank you very much for your time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured