开发者

How do I query a database for only one field of a resultset? (JAVA ODBC)

I have an assignment I am working on (and trying to debug). I have a JComboBox which I need to fill with only ISBN numbers. The Database returns ISBNs, BookTitle, QuantityOnHand, and Price. I am to make a method (loadCombo) which is to load the comboBox with (as I said) only the ISBN numbers. I开发者_Go百科 have this little snippet written:

    public static void loadCombo(JComboBox box)
{
   String query = "SELECT * FROM Books";

   try
   {
       result = statement.executeQuery(query);
       result.getString(1);            
       addISBN(result,box);
   }
   catch(SQLException sqlex)  {sqlex.printStackTrace();}
    public static void addISBN(ResultSet result,JComboBox box)
{
    try
    {
         while(result.next()) {box.addItem(result);}
    }
    catch(SQLException e)   {e.printStackTrace();}
}

However, this throws a load of errors. What am I doing wrong here? I thought that I was making a resultSet (result), then getting the first field (result.getString(1)), and then throwing the resultset and combobox to my addISBN method which will add the resultset to my comboBox?

So I did what the first answer said (thank you for the advice, I guess I should read my assignments a little more carefully!). However, I now crash on the addISBN method. My ISBN textbox has 14 lines, all say

'sun.jdbc.odbc.JdbcOdbcReseltSet@13f9460'


Your UI and combo box should not come within one hundred yards of persistence code. It's called layering or separation of concerns. Tell your professor that it's considered a best practice in the real world.

This code is heinous. I'd write it more like this. Notice how I close those resources in the scope of the method in which they were created. That's important. No JDBC classes should leak out of the persistence package. Load the results into a data structure (in this case, a List of Strings for ISBNs).

Java's an object-oriented language. Where's the Book model class?

package persistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * BookDao
 * @author Michael
 * @since 4/6/11
 */
public class BookDao
{
    public static final String SELECT_ALL_BOOKS = "SELECT * FROM Books";

    private Connection connection;

    public BookDao(Connection connection)
    {
        this.connection = connection;
    }

    public List<String> find() throws SQLException
    {
        List<String> isbn= new ArrayList<String>();

        Statement statement = null;
        ResultSet result = null;

        try
        {
            statement = this.connection.createStatement();
            result = statement.executeQuery(SELECT_ALL_BOOKS);
            while (result.next())
            {
                isbn.add(result.getString(1));
            }
        }
        finally
        {
            close(result);
            close(statement);
        }

        return isbn;
    }

    private static void close(ResultSet result)
    {
        try
        {
            if (result != null)
            {
                result.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    private static void close(Statement statement)
    {
        try
        {
            if (statement != null)
            {
                statement.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}


REmove the result.getString(1); line. With a ResultSet, you need to do a next() before you can access the resultset elements, as there is no current row prior to a next();

Also, for the future, you are much more likely to get good responses if you post the exception details.


Might want to construct a proper SQL statement. Instead of SELECT * FROM Books, use SELECT [column name in db] FROM Books

edit: What unit wrote in his comment.


String myStr = result.getString(1);

Then use myStr instead of result. use result only to navigate to the next row.

Reference - ResultSet

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜