Issue
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" :
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.
Solution
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 String
s.
Answered By - geco17
Answer Checked By - Mildred Charles (JavaFixing Admin)