I am developing a JavaFx Application and I have to use CRUD for several model classes. I want to create a generic interface and then would like to implement for those classes. I couldn't find any example where it is Implemented with JavaFX. I am not using DAO or Hibernate it is just JDBC connection.
What I have done so far
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();
}
Implementation:
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);
}
}
}
}
I am stuck and unable to pass the object of model class to create data. Any help or suggestions would be highly appreciated.
Thanks
Edited Signature of my create method in CRUD Interface is
void create(T t);
Now I need to replace T with my model class while Implementing the interface.
i.e. Product, Category, SubCategory etc.
I have several Classes with the CRUD and I do not want to repeat these methods in each of the Java Class separately therefor I need to create a Generic Interface.