ieri riguardando la classe che rappresenta il mio db mi sono chiesto se quale fosse il metodo migliore per gestire le eccezioni.
ho letto sia il capitolo del libro che un pò di articoli in giro, ma sono tutti sulla stessa lunghezza d'onda: dicono quali siano gli strumenti ma non come sia meglio usarli.
quindi chiedo a voi che ne sapete gran lunga più di me.
vi posto il "modo" in cui le ho gesite fino ad oggi e vi chiedo consiglio su come migliorare il mio approccio.
questa una estrapolazione della classe db:
codice:
public void select(String wName, String wAuthor, String wEditor, String wIsbn) throws ClassNotFoundException, SQLException {
conn = DBManager.getInstance().getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = null;
String sql = null;
if ((wName.length() == 0) && (wAuthor.length() == 0) && (wEditor.length() == 0) && (wIsbn.length() == 0)) {
sql = "SELECT * FROM bookv";
} else if ((wName.length() != 0) && (wAuthor.length() == 0) && (wEditor.length() == 0) && (wIsbn.length() == 0)) {
sql = "SELECT * FROM bookv WHERE name LIKE '%" + wName + "%'";
} else if ((wName.length() == 0) && (wAuthor.length() != 0) && (wEditor.length() == 0) && (wIsbn.length() == 0)) {
sql = "SELECT * FROM bookv WHERE author_name LIKE '%" + wAuthor + "%'";
} else if ((wName.length() == 0) && (wAuthor.length() == 0) && (wEditor.length() != 0) && (wIsbn.length() == 0)) {
sql = "SELECT * FROM bookv WHERE editor_name LIKE '%" + wEditor + "%'";
} else if ((wName.length() == 0) && (wAuthor.length() == 0) && (wEditor.length() == 0) && (wIsbn != null)) {
sql = "SELECT * FROM bookv WHERE isbn LIKE '%" + wIsbn + "%'";
}
rs = stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("book_id");
String name = rs.getString("name");
String author = rs.getString("author_name");
String editor = rs.getString("editor_name");
String price = rs.getString("price");
String isbn = rs.getString("isbn");
String note = rs.getString("note");
Object[] riga = {id, name, author, editor, price, isbn, note};
((DefaultTableModel) FormMain.getTable().getModel()).addRow(riga);
}
rs.close();
stmt.close();
}
public void insert(String name, int author_id, int editor_id, double price, String isbn, String note) throws ClassNotFoundException, SQLException {
String sql = "INSERT book (name, author_id, editor_id, price, isbn, note) VALUES ('" + name + "', " + author_id + ", " + editor_id + ", " + price + ", '" + isbn + "', '" + note + "')";
conn = DBManager.getInstance().getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
public void update(int id, String name, int author_id, int editor_id, double price, String isbn, String note) throws ClassNotFoundException, SQLException {
String sql = "UPDATE book SET name = '" + name + "', author_id = " + author_id + ", editor_id = " + editor_id + ", price = " + price + ", isbn = '" + isbn + "', note = '" + note + "' WHERE book_id = " + id;
conn = DBManager.getInstance().getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
come vedete qua ho usato sempre la clausola throws.
poi nei punti dove richiamo questi metodo (tipicamente eventi in JFrame):
codice:
try {
String name = textBookName.getText().toLowerCase().trim();
Author author = (Author) comboAuthor.getSelectedItem();
int author_id = author.getId();
Editor editor = (Editor) comboEditor.getSelectedItem();
int editor_id = editor.getId();
double price = Double.parseDouble(textAddPrice.getText());
String isbn = textIsbn.getText().toLowerCase().trim();
String note = areaNote.getText().trim();
dbman.insert(name, author_id, editor_id, price, isbn, note);
JOptionPane.showMessageDialog(null, "Book added: " + textBookName.getText());
textBookName.setText("");
comboAuthor.setSelectedIndex(0);
comboEditor.setSelectedIndex(0);
textAddPrice.setText("");
textIsbn.setText("");
areaNote.setText("");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
uso i try/catch.
adesso: come posso migliorare la gestione delle eccezioni??