是否可以在JavaFX中创建通用CRUD

时间:2019-02-26 08:26:23

标签: java generics javafx generic-interface

我正在开发JavaFx应用程序,必须将CRUD用于多个模型类。我想创建一个通用接口,然后为这些类实现。我找不到任何使用JavaFX实现的示例。我不使用DAO或Hibernate,它只是JDBC连接。

到目前为止我所做的

public interface CrudInterface<T, PK extends Serializable> {
    void create(T t);
    T read(PK id);
    void update(T t);
    void delete(T t);
    ObservableList<T> getAll();
}

实施:

    public class ProductImplementation implements CrudInterface, ProductInterface{

    //Other Methods
.
.
.
        @Override
            public void create(Product product) {
                try {
                    DatabaseConnection localConnection = new DatabaseConnection();
                    connection = localConnection.getLocalConnection();
                    connection.setAutoCommit(false);
                    String query = "INSERT INTO products (product_name, bar_code, product_size, product_cost, product_net_dealer_price, productQuantity, product_alert_quantity, product_tax, product_image, product_invoice_detail, product_category_id, product_subcategory_id, suplier_name) VALUES  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    preparedStatement = connection.prepareStatement(query);
                    preparedStatement.setString(1, product.getName());
                    preparedStatement.setString(2, product.getBarcode());
                    preparedStatement.setString(3, product.getSize());
                    preparedStatement.setInt(4, product.getPrice());
                    preparedStatement.setString(5, product.getNetDealerPrice());
                    preparedStatement.setInt(6, product.getQuantity());
                    preparedStatement.setInt(7, product.getAlertQuantity());
                    preparedStatement.setInt(8, product.getTax());
                    preparedStatement.setString(9, product.getImage());
                    preparedStatement.setString(10, product.getProductInvoiceDetail());
        //            preparedStatement.setInt(11, product.getCategories().getCategoryID());
        //            preparedStatement.setInt(12, product.getSubCategories().getSubCategoryID());
        //            preparedStatement.setInt(13, product.getSuppliers().getSupplierID());
                    preparedStatement.execute();
                    connection.commit();
                } catch (SQLException e) {
                    try {
                        connection.rollback();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductImplementation.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Logger.getLogger(ProductImplementation.class.getName()).log(Level.SEVERE, null, e);
                } finally {
                    try {
                        connection.close();
                        preparedStatement.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(ProductImplementation.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }

我被困住了,无法传递模型类的对象来创建数据。任何帮助或建议,将不胜感激。

谢谢

已编辑 我在CRUD界面中创建方法的签名是

void create(T t);

现在,在实现接口时,我需要用模型类替换T。 即ProductCategorySubCategory

我有几个带有CRUD的类,并且我不想在每个Java类中分别重复这些方法,因此我需要创建一个通用接口

0 个答案:

没有答案