내가 이것을 묻는 이유는 SQL에서 복잡한 관계형 데이터베이스를 설정하는 것보다 Java에서 파일 관리 시스템을 설정하는 것이 훨씬 쉽기 때문입니다. 어떤 것 인 SQL 관계형 데이터베이스있는 것은 다음의 예를 통해 나를 혜택 :Java 클래스는 SQL 관계형 데이터베이스를 대체하는 좋은 방법입니까?
예 :
public class Product implements Serializable {
public static int productCount = 0;
private final int productID;
private final long productRegisterDate;
private String productDescription;
private List<ProductPrice> productPrices;
Product (String desc) {
productID = ++productCount;
productRegisterDate = Calendar.getInstance().getTimeInMillis();
productDescription = desc;
productPrices = new ArrayList<ProductPrice>();
}
public Product addPrice(double price, String priceDesc) {
productPrices.add(new ProductPrice(price, priceDesc);
return this;
}
public void updateProduct(String desc, ArrayList<ProductPrice> prices) {
productDescription = desc;
productPrices = prices;
}
public int getID() return productID;
public long getDateRegistered() return productRegisterDate;
public String getDescription() return productDescription;
public ArrayList<ProductPrice> getPrices() return productPrices;
}
public class ProductPrice implements Serializable {
private double price;
private String priceDescription;
ProductPrice(double price, String priceDesc) {
this.price = price;
priceDescription = priceDesc;
}
}
삽입 제품 :
같은 제품 자바가변 가격을 수있는 제품 파일을
...
List<Product> myProducts = new ArrayList<Product>();
myProducts.add(new Product("product a")
.addPrice(1.99,"250g pack")
.addPrice(2.99,"500g pack");
찾기 제품 제품 :
객체 1,123,210쿼리 제품 정보 :
public boolean updateProductByID(int pid, String desc, ArrayList<ProductPrice> prices) {
Product p = null;
try {
p = findProductById(pid);
p.updateProduct(desc, prices);
} catch (NullPointerException e) {
return false;
}
return true;
}
저장/데이터를로드 :
이...
private final String SAVE_PATH = "C:/";
private final String PRODUCTS_FILE = "Products.dat";
public static boolean saveProducts(ArrayList<Product> myProducts) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(SAVE_PATH+PRODUCTS_FILE));
out.writeObject(myProducts);
out.close();
} catch (IOException e) {
return false;
}
return true;
}
public static boolean loadProducts(ArrayList<Product> myProducts) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(SAVE_PATH+PRODUCTS_FILE));
myProducts = (ArrayList<Product>) in.readObject();
return true;
} catch (IOException e1) {
return false;
} catch (ClassNotFoundException e2) {
return false;
}
}
나에게 알려 주시기 바랍니다, 내가 알고 싶은 목적없이
...
String productDescription = p.getDescription();
업데이트 제품 정보 모든 세부 사항. 클래스 개체 (예 : 제품, 주문 등)가있는 Java 응용 프로그램이 이미 있는데 로컬 파일 시스템 대신 데이터를 제공하고 저장하려면 데이터베이스를 사용해야하는지 궁금합니다.
그래서 파일 기반 옵션은 동시성과 ACID 및 데이터베이스를 처리 할 수 있습니까? 이들은 RDBMS가 수행 할 수있는 광고 호크 (ad-hock) 쿼리를 처리 할 수 있습니까? – Oded
그리고 임의의 DB /보고 도구를 통해 액세스 할 수 있습니까? –
'find product by id' 메쏘드를 사용한다면 인덱스를 가지고 있어야합니다. 자바에서는 해시 맵을 직접 해보려한다면 해시 맵을 추천한다. – corsiKa