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.