in verità l'operazione già la so fare:
codice:private DefaultTableModel model; private Object[][] data = new Object[][]{}; private String[] head = new String[]{ "Book ID", "Book name", "Author", "Editor", "Price", "ISBN", "Note" }; ....... model = new myTableModel(data, head); tableData.setModel( model ); tableData.setAutoCreateRowSorter(true); tableData.setCellSelectionEnabled(true); ....... private class myTableModel extends DefaultTableModel { public myTableModel(Object[][] data, String[] col) { super(data, col); } @Override public boolean isCellEditable(int row, int column) { return false; } }ma nn mi piace molto perchè vorrei separare totalmente la parte grafica e quella logica.codice:import java.sql.*; import javax.swing.table.DefaultTableModel; import libreria.*; public class DoSelect { public static void leggi() throws ClassNotFoundException, SQLException { Connection conn = DoConnection.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM bookv"); 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) Main.getTable().getModel()).addRow(riga); } rs.close(); stmt.close(); } }
ho però alcuni problemi.
io ho provato intanto a fare questo.
classe che rappresenta i campi del db:
select:codice:public class Book { private int book_id; private String name; private String author; private String editor; private double price; private String isbn; private String note; public Book(int book_id, String name, String author, String editor, double price, String isbn, String note) { this.book_id = book_id; this.name = name; this.author = author; this.editor = editor; this.price = price; this.isbn = isbn; this.note = note; } public int getBookId() { return book_id; } public String getName() { return name; } public String getAuthor() { return author; } public String getEditor() { return editor; } public double getPrice() { return price; } public String getIsbn() { return isbn; } public String getNote() { return note; } }
il mio problema è da qui.codice:import java.sql.*; import java.util.ArrayList; public class RiempiTable { public static ArrayList data() throws ClassNotFoundException, SQLException { ArrayList list = new ArrayList(); Connection conn = DoConnection.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM bookv"); while (rs.next()) { int book_id = rs.getInt("book_id"); String name = rs.getString("name"); String author = rs.getString("author_name"); String editor = rs.getString("editor_name"); double price = rs.getDouble("price"); String isbn = rs.getString("isbn"); String note = rs.getString("note"); list.add(new Book(book_id, name, author, editor, price, isbn, note)); } rs.close(); stmt.close(); return list; } }
nn riesco a richiamare questa classe per riempire la jtable.
(sempre se ho fatto giusto.....).

Rispondi quotando