ho queste jcombobox:
nella finestra vengono visualizzati i nomi.codice:public class DoFillCBAuthor { private int id; private String name; public DoFillCBAuthor(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String toString() { return name; } public static void fillAuthor() throws SQLException, ClassNotFoundException { Connection conn = null; Statement stmt = null; ResultSet rs = null; conn = DoConnection.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM author"); while (rs.next()) { FramePrincipale.getComboAuthor().addItem(new DoFillCBAuthor(rs.getInt("author_id"), rs.getString("author_name"))); } rs.close(); stmt.close(); } }
però quando devo mandare la query devo fare in modo che prenda l'id.
il metodo per l'inserimento è questo:
i dati glieli passerei così:codice:public class DoInsert { public static void insert(String name, int author_id, int editor_id, double price, String isbn, String note) throws ClassNotFoundException { Connection conn = null; Statement stmt = null; try { conn = DoConnection.getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("INSERT book (name, author_id, editor_id, price, isbn, note) VALUES ('" + name + "', " + author_id + ", " + editor_id + ", " + price + ", '" + isbn + "', '" + note + "'"); } catch (SQLException e) { JOptionPane.showMessageDialog(null, e.getMessage() + "\n" + stmt); } finally { try { if (!stmt.isClosed()) { stmt.close(); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } } } }
ottengo sempre errore però.codice:try { String name = textBookName.getText(); int author = comboAuthor.getSelectedIndex(); int editor = comboEditor.getSelectedIndex(); double price = Double.parseDouble(textAddPrice.getText()); String isbn = textIsbn.getText(); String note = areaNote.getText(); DoInsert.insert(name, author, editor, price, isbn, note); textBookName.setText(""); comboAuthor.setSelectedIndex(0); comboEditor.setSelectedIndex(0); textAddPrice.setText(""); textIsbn.setText(""); areaNote.setText(""); } catch (ClassNotFoundException ex) { JOptionPane.showMessageDialog(panelContainer, ex.getMessage()); }
come faccio a passargli il valore id??

Rispondi quotando