mardi 5 mai 2015

How to populate javafx TableView column with values from MySQL generated column?

I am using generated columns introduced in MySQL version 5.7. How to use values from those columns to populate a javafx TableView? Excerpts from the code which I am using to populate the TableView:

public void processQuery(){
    try {
        int i = (Integer)aList.get(arrayObjectNumber);
        System.out.println(i);
        String aQuery = "select * from sale_invoice_details "
                        + "where sale_invoice_id = "+  i;
        rs = con.createStatement()
                .executeQuery(aQuery);
        System.out.println("executed aQuery");
        while(rs.next()) {
            System.out.println(rs);
        sale_invoice_details_array
                 .add(new s_i_d
                        (rs.getInt("id"), 
                        rs.getString("name"), 
                        rs.getFloat("quantity"),
                        rs.getFloat("amount_generated")));}
        processQueryB(i);
         } catch (Exception e) {
        System.out.println(e);
    }
}
  ...

public class s_i_d {
    private IntegerProperty id;
    private StringProperty name;
    private FloatProperty quantity;
    private FloatProperty amount_generated;
    private s_i_d(Integer id, String name, Float quantity, Float amount_generated) {
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty(name);
        this.quantity = new SimpleFloatProperty(quantity);
        this.amount_generated = new SimpleFloatProperty(amount_generated);
    }
    public IntegerProperty idProperty() {return id;}
    public StringProperty nameProperty() {return name;}
    public FloatProperty quantityProperty() {return quantity;}
    public FloatProperty amount_generatedProperty() {return amount_generated;}
}

In the console:

    ...
   executed aQuery
   com.mysql.jdbc.JDBC4ResultSet@248423af
   java.sql.SQLException: Column 'amount_generated' not found.
    ...

MySQL table code:

CREATE TABLE `sale_invoice_details` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `rate` float DEFAULT NULL,
  `quantity` float DEFAULT NULL,
  `discount` float DEFAULT NULL,
  `amount` float DEFAULT NULL,
  `sale_invoice_id` int(11) DEFAULT NULL,
  `product_id` int(11) DEFAULT NULL,
  `amout_generated` float GENERATED ALWAYS AS (quantity*rate) VIRTUAL,
  PRIMARY KEY (`id`),
  KEY `fk_sale_invoice_id_idx` (`sale_invoice_id`),
  KEY `fk_product_id_idx` (`product_id`),
  CONSTRAINT `fk_product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_sale_invoice_id` FOREIGN KEY (`sale_invoice_id`) REFERENCES `sale_invoice` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=32303 DEFAULT CHARSET=latin1$$

Aucun commentaire:

Enregistrer un commentaire