Categories
Foundations of Programming

FIT9017: Week 2

Today Judy Sheard continued the introduction to the Java language whilst maintaining a conceptual anchor. Having worked through several Java text books prior to starting the course I was already familiar with most of the terms and concepts but a little revision never hurts. The topics covered in the lecture:

  • Classes
  • Fields
  • Assignment
  • Expressions
  • Constructors
  • Methods
  • Parameters
  • Accessor/Mutator methods
  • Data types
  • Display Information
  • Visibility Modifiers

The tutorial included some revision of the material presented in the lecture and some references to the text book which no one brought. This was a bit annoying, why they could not list the questions instead of referencing seems irrational. Similarly the decision not to make available the lecture notes prior to the lecture so students can preview it is really odd…

Anyhow, as the lecture and tut were quite basic I had some time to work on my own project in which I identified a bug. A JTable which retrieves selected data from a MySQL table and then replaces it has a bug in the update function whereby a single array variable is dropped from the end of the first row. The bug must be either in the code pulling the values from the JTable into an Array variable or the code creating an SQL update Query from the Java array:

JTable -> Array variable code:

private void updateQuoteButtonMouseReleased(java.awt.event.MouseEvent evt) {
for (int a=0;a < qData.arrayRet().length;a++)
{
for (int b=0;b < 3;b++)
{
String cString = (String) quoteTable.getValueAt(a,b);
qData.setArrayVal(a,b,cString);
}
}
try {
qData.updateBookDB();
this.qPop(Integer.parseInt(enterIDField.getText()));
} catch (SQLException ex) {
Logger.getLogger(BookPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BookPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}

Array variable -> SQL UPDATE query code:

public void updateBookDB()
throws SQLException, IOException, ClassNotFoundException
{
if (qArray.length == 0)
{
SimpleDataSource.init();
Connection conn = SimpleDataSource.getConnection();
try
{
java.sql.PreparedStatement
stat2 = conn.prepareStatement
("DELETE FROM quotes WHERE book_ID = ?");
stat2.setInt(1, bookID);
stat2.execute();
}
finally
{
conn.close();
}
}
else
{
String SQLCmd = "INSERT INTO quotes values ";
for (int a=0;a < (qArray.length - 1);a++)
{
SQLCmd = SQLCmd + "(" + bookID + ", 1, " +
qArray[a][0] + ", '" +
qArray[a][1] + "', '" +
qArray[a][2] + "'),";
}
SQLCmd = SQLCmd + "(" + bookID + ", 1, " +
qArray[qArray.length - 1][0] + ", '" +
qArray[qArray.length - 1][1] + "', '" +
qArray[qArray.length - 1][2] + "');";
SimpleDataSource.init();
Connection conn = SimpleDataSource.getConnection();
try
{
java.sql.PreparedStatement
stat2 = conn.prepareStatement
("DELETE FROM quotes WHERE book_ID = ?");
stat2.setInt(1, bookID);
java.sql.PreparedStatement
stat3 = conn.prepareStatement(SQLCmd);
stat2.execute();
stat3.execute();
}
finally
{
conn.close();
}
}

After having a look at the code in notepad and being unable to find the bug. I will look for the error tomorrow night and see how long it takes me to find and fix. I won’t open another of my own projects in BlueJ after it automatically renamed my Package declarations making errors throughout my source code.

One reply on “FIT9017: Week 2”

Leave a Reply

Your email address will not be published. Required fields are marked *