1

I've got a multi-class project in Java which connects with MySQL, and I can establish a connection with this SQL database, but when I try to read it into an array it comes out with nonsense rather than the information requested. What am I doing wrong?

public DVDCollection getDVDs(int sortOrder)
{
    DVDCollection dvdcollection = new DVDCollection();
    Connection conn = getConnection();
    if (conn != null)
    {            
        String sql;
        if (sortOrder==1)
        {
            sql = "SELECT * FROM dvds ORDER BY title";
        }
        if (sortOrder==2)
        {
            sql = "SELECT * FROM dvds ORDER BY dvds.id";
        }
        else
        {
            sql = "SELECT * FROM dvds ORDER BY dvds.year";
        }
    
        try
        {
            //PreparedStatement ps1 = conn.prepareStatement(sql); 
            ResultSet rs1 = conn.prepareStatement(sql).executeQuery();
            while (rs1.next()) //get each row from the resultset
            {   //create an DVD object to add to ArrayList
                dvdcollection.addDVD(new DVD(rs1.getInt("id"), rs1.getString("title"), rs1.getInt("year"), rs1.getBoolean("favourite")));                          
            }          
            rs1.close();
            //ps1.close();
            closeConnection(conn);
            return dvdcollection;
        }
        catch (SQLException ex)
        {
            System.out.println(ex);     
            return dvdcollection;                    
        } 
        finally
        {
            closeConnection(conn);
        }
    }
    else
    {
        return dvdcollection;
    }
        
}

It comes up with a squiggly line under the sql definitions, saying that they're never read, but I just can't work out why that is! It builds fine and can see that there are rows in the database but it returns this:

image of GUI displaying "java2assignment3.DVD@5e0010bc" and "java2assignment3.DVD@1168d798" :

enter image description here

When I click on the objects, the string of numbers and letters refresh, and, I'm furthermore not able to run my "Add DVD", "Edit DVD" and "Delete DVD" functions because the latter two require being able to select an object, and "Add DVD" returns "Application error, DVD not added, please contact IT Administration" my error text which should only happen if this else condition is met:

DVD dvd = service.addDVD(newDVD);
if (dvd != null)
{
    txtResults.setText(txtTitle.getText() + " sucessfully added");
    parent.loadData();
}
else
{
    txtResults.setText("Application error, DVD not added, please contact IT Administration");                        
}

Would appreciate any help anyone can offer on this. I am hoping that once I resolve what's happening in the first instance then the second issue will resolve itself or be related, but would love advice on either.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • `toString()` (and `System.out.println(someObject)` returns something like this by default. you need to overwrite the `toString` method of `DVD`. – dan1st Nov 14 '21 at 07:26
  • This is [`Object::toString`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString())'s implementation. To get a human readable representation, we can override this method. – Turing85 Nov 14 '21 at 07:28
  • @dan1st & Turing85 thank you so much that is super helpful, I have fixed that particular error!!!! I still can't create new DVDs, edit or delete them, but that's a different issue and I'll need to find a different solution. – crab_attack Nov 14 '21 at 08:22

1 Answers1

0

The code you're using to get the list of dvds from the database is fine but you need to take a look at your dvd object and how it's being rendered.

It's not clear from the image posted with the question what the GUI is but the way the dvds are displayed corresponds with java's default toString() method.

One way around this problem is therefore to override the toString() method on your dvd class:

class DVD {
    // the usual stuff, getters, setters, member variables
    @Override
    public String toString() {
        return id + " " + title;
    }
}

Another is to write a method to render it in the list. For example, if your GUI list can take String objects directly instead of dvds, you could have a method somewhere like:

static stringForList(DVD dvd) {
    return dvd.getId() + " " + dvd.getTitle();
}

using whatever fields or getters / setters you have.

Then when you're populating the list, instead of adding DVD objects, wrap them:

yourList.add(stringForList(dvd))

with yourList declared such that it takes Strings.

geco17
  • 5,152
  • 3
  • 21
  • 38