Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] Rimpire JTable da mysql

    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;
            }
        }
    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();
        }
    }
    ma nn mi piace molto perchè vorrei separare totalmente la parte grafica e quella logica.
    ho però alcuni problemi.
    io ho provato intanto a fare questo.
    classe che rappresenta i campi del db:
    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;
        }
    }
    select:
    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;
        }
    }
    il mio problema è da qui.
    nn riesco a richiamare questa classe per riempire la jtable.
    (sempre se ho fatto giusto.....).

  2. #2
    nessuno ha un'idea??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.