2011-12-10 2 views
0

내가 이것을 묻는 이유는 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 응용 프로그램이 이미 있는데 로컬 파일 시스템 대신 데이터를 제공하고 저장하려면 데이터베이스를 사용해야하는지 궁금합니다.

+9

그래서 파일 기반 옵션은 동시성과 ACID 및 데이터베이스를 처리 할 수 ​​있습니까? 이들은 RDBMS가 수행 할 수있는 광고 호크 (ad-hock) 쿼리를 처리 할 수 ​​있습니까? – Oded

+1

그리고 임의의 DB /보고 도구를 통해 액세스 할 수 있습니까? –

+0

'find product by id' 메쏘드를 사용한다면 인덱스를 가지고 있어야합니다. 자바에서는 해시 맵을 직접 해보려한다면 해시 맵을 추천한다. – corsiKa

답변

4

정말로 원한다면 원하는대로 재발 명할 수 있습니다.

장점 :

  • 당신은 많은 것을 배울.
  • 시스템을 완전히 제어 할 수 있습니다.

단점 :

  • 당신은 시스템을 완전히 제어 할 수 있습니다.
  • 당신이 재발견하는 시스템을 만들고 살아가는 사람들의 경험을 잃게됩니다.
  • 자신이 발견 한 모든 버그를 수정해야합니다.
  • 직접 테스트해야합니다 (예 : 수백만 명이 이미 RDBMS를 사용하고 있습니다).
  • 문제점을 발견하면 데이터베이스 시스템인지 또는이를 사용하는 시스템인지 여부를 알 수 없습니다.

그래서 질문을 작성하는 데 얼마의 시간을 할애하고 있습니까?

0

Oded가 매우 잘 언급되었으므로 동시성, ACID 및 ad-hock 쿼리가 DB를 원하는 몇 가지 이유입니다. 정렬, 고유 제한 조건, 관계, 복제 및 DB 엔진이 제공하는 다른 많은 도구를 추가 할 것입니다.

개체 작업을 좋아한다면, SQLLite와 같이 DB를 설치하는 데 아주 간단하게 최대 절전 모드를 시작할 수 있습니다.응용 프로그램이 커지면 SQLLite에서 MySQL, PostgreSQL 또는 Oracle으로 쉽게 이동할 수 있습니다.

1

SQLLite + Hibernate를 사용하면 코드가 훨씬 단순 해집니다. 나는 당신의 현재 접근 방식이 동시성, 일관성, 관계 유지 등등과 같은 일반적인 db 문제에 대한 관점을 제공 할 것이라고 생각합니다. 이들 중 하나를 사용할 계획이 없다면 순수 Java로 솔루션을 구현할 수 있습니다. 그러나, 당신이 내가 위에서 말한 것에서 이익을 얻을 수 있다고 생각한다면, 당신의 필요에 따라 DB를 사용하는 것이 낫습니다. 그러나 내가 믿는 것처럼 교육적 목적으로하고 있다면, 당신의 접근 방식을 고수하십시오.

관련 문제