我正在开发一个 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
在实现接口时用我的模型类替换。即Product
,等Category
_SubCategory
我有几个带有 CRUD 的类,我不想在每个 Java 类中分别重复这些方法,因此我需要创建一个通用接口。