HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Database assignment with MySQL and Swing

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withdatabaseassignmentmysqlswingand

Problem

I have completed this assignment from Stanford CS108 on MySQL and Swing (it's part B). It would be wonderful if someone could point out my weak spots in the code and overall design.

I have uploaded code on GitHub.

Task:


In this part of the assignment you will create a GUI-based (Graphical
User Interface) application which will allow a user to access the
example metropolises database from the Database handout.


The application will provide options allowing the user to either
search for metropolises matching particular criteria or to add a new
metropolis to the database. The application should include a JTable
which displays information gathered from the metropolises database.



I ended up with this:

JMetropolisViewer.java

```
package databaseexercise;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;

public class JMetropolisViewer extends JFrame {

public static final String APP_TITLE = "Metropolis Viewer";
public static final String METROPOLIS = "Metropolis";
public static final String POPULATION = "Population";
public static final String CONTINENT = "Continent";

//index[0] must be "larger than"
private static final String[] optionsForPopulation = {"Population larger than", "Population less than"};
//index[0] must be "partial match"
private static final String[] optionsForMatch = {"Partial match", "Exact match"};

private List metropolisList = new ArrayList();
private Database db;
private MetropolisTableModel tableModel;

private JTextField cityNameInput;
private JTextField continentInput;
private JTextField populationInput;

private JButton addButton;
private JButton searchButton;

private JComboBo

Solution

getValueAt: Bounds check

if(row = cityList.size()) return null; doesn't seem right to me. Here is how the DefaultTableModel handles it:

/**
 * [...]
 *
 * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
 *               column was given
 */
public Object getValueAt(int row, int column) {
    Vector rowVector = (Vector)dataVector.elementAt(row);
    return rowVector.elementAt(column);
}


I think throwing a ArrayIndexOutOfBoundsException seems like a good idea. Alternatively, you could also store default values somewhere and return those (eg "missing", "n/a", "", "invalid column", etc).

Misc

  • You print quite a lot in your classes. Try to handle this via return values or exceptions instead.



  • In your finally block, you should check if connection is null before closing it.



  • variables should have as small a scope as possible. For example, preparedStatement doesn't need to be at class level.



  • you use prepared statements in add, why not use them in search as well.



  • try not to throw or catch the generic Exception, but all the specific exceptions, so that the calling method can handle each as it wants to.

Code Snippets

/**
 * [...]
 *
 * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
 *               column was given
 */
public Object getValueAt(int row, int column) {
    Vector rowVector = (Vector)dataVector.elementAt(row);
    return rowVector.elementAt(column);
}

Context

StackExchange Code Review Q#69321, answer score: 3

Revisions (0)

No revisions yet.