Java JDBC DAO Code Review: Fixing Bakker, Sterke Dranken & Boeken Classes

Computer science6.84 KB

Java JDBC DAO and Swing Code: Corrected Version

Below is the corrected code for the BakkerDAO, BaseDAO, sterke dranken, boeken and Nachtwinkel classes. The main issues addressed are a missing prepareStatement assignment, incorrect use of ps before initialization, and a few spelling and capitalization inconsistencies.

1. BakkerDAO

public class BakkerDAO extends BaseDAO {
PreparedStatement ps = null;

public boolean verwijderBakker(Bakker b) {
String sql = "DELETE FROM bakker WHERE id = ?";
try {
Connection c = getCon();
if (c == null || c.isClosed()) {
throw new IllegalStateException("Connection closed or failed");
}
ps = c.prepareStatement(sql);
ps.setInt(1, b.getID());
int result = ps.executeUpdate();
if (result > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return true;
}

public HashSet geefAlleBakkers() {
HashSet lijst = new HashSet();
Statement st = null;
try {
Connection c = getCon();
if (c == null || c.isClosed()) {
throw new IllegalStateException("Geen connection");
}
st = c.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM Bakker");
while (rs.next()) {
Bakker b = new Bakker(rs.getString("naam"), rs.getInt("leeftijd"));
lijst.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
}
return lijst;
}

public static void main(String[] args) {
BakkerDAO b = new BakkerDAO();
HashSet lijst = b.geefAlleBakkers();
for (Object ba : lijst) {
System.out.println(((Bakker) ba).getNaam());
}
}
}

2. BaseDAO

public class BaseDAO {
private Connection con;

public BaseDAO() {
try {
con = DatabaseSingleton.getDatabaseSingleton().getConnection(true);
} catch (SQLException e) {
e.printStackTrace();
}
}

public Connection getCon() {
return con;
}

public void setCon(Connection con) {
this.con = con;
}
}

3. Sterke dranken and boeken methods

public ArrayList geefAlleSterkeDranken() {
ArrayList lijst = new ArrayList();
Statement st = null;
try {
Connection c = getCon();
if (c == null || c.isClosed()) {
throw new IllegalStateException("Connection onverwacht beëindigd");
}
st = c.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM Product");
while (rs.next()) {
Sterkedrank p = new Sterkedrank(rs.getString("Naam"), rs.getDouble("Prijs"), rs.getInt("ID"));
lijst.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return lijst;
}

public int insert(BoekVO boekVO) {
PreparedStatement ps = null;
String sql = "INSERT INTO boeken VALUES(?,?,?)";
try {
if (getConnection().isClosed()) {
throw new IllegalStateException("Error unexpected");
}
ps = getConnection().prepareStatement(sql);
ps.setLong(1, boekVO.getIsbn());
ps.setString(2, boekVO.getTitel());
ps.setString(3, boekVO.getAuteur());
return ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new RuntimeException(e.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new RuntimeException("Error.unexpected");
}
}
}

public ArrayList selectAll() {
ArrayList list = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * FROM boeken";
try {
if (getConnection().isClosed()) {
throw new IllegalStateException("Error unexpected");
}
ps = getConnection().prepareStatement(sql);
rs = ps.executeQuery();
list = new ArrayList();
while (rs.next()) {
list.add(fillVO(rs));
}
return list;
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new RuntimeException(e.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new RuntimeException("Error.unexpected");
}
}
}

4. Nachtwinkel (Swing application)

public class Nachtwinkel extends JFrame {
private static final long serialVersionUID = -6707664468869668584L;
private Klant k = new Klant(14);
private JComboBox productenBox = null;
private JComboBox mand = null;

public Nachtwinkel() {
this.getContentPane().setLayout(new GridLayout(2, 5, 2, 5));
this.add(getProductenBox());
this.add(getMand());
this.add(getToevoegenKnop());
this.add(getMaakFactuurKnop());
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

private JComboBox getMand() {
if (mand == null) {
mand = new JComboBox();
}
return mand;
}

private JComboBox getProductenBox() {
if (productenBox == null) {
productenBox = new JComboBox();
vulBox(productenBox);
}
return productenBox;
}

private JButton getMaakFactuurKnop() {
JButton b = new JButton("Maak factuur");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Nachtwinkel.this.k.maakFactuur();
}
});
return b;
}

private JButton getToevoegenKnop() {
JButton b = new JButton("Toevoegen");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ProductForCombo p = (ProductForCombo) Nachtwinkel.this.productenBox.getSelectedItem();
try {
Nachtwinkel.this.k.voegProductToe(p.getProduct());
mand.addItem(p);
} catch (MinderjarigExceptie e) {
JOptionPane.showMessageDialog(Nachtwinkel.this, "Minderjarig!");
}
}
});
return b;
}

private void vulBox(JComboBox producten) {
producten.removeAllItems();
ProductDAO pdao = new ProductDAO();
for (Product p : pdao.geefAlleSterkeDranken()) {
producten.addItem(new ProductForCombo(p));
}
}

public static void main(String[] args) {
new Nachtwinkel();
}
}

Download

PDF Word HTML