pablohf

Untitled

Nov 12th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package trabfinal;
  6.  
  7. /**
  8.  *
  9.  * @author pablo
  10.  */
  11. public class TrabFinal {
  12.  
  13.     /**
  14.      * @param args the command line arguments
  15.      */
  16.     public static void main(String[] args) {
  17.         Database db = new Database();
  18.  
  19. //        Book b1 = new Book("Pablo", "Java", "987598759", "Vol. 1", "2B", "O'Reilly");        
  20. //        db.insertBook(b1);
  21.  
  22.        
  23.         db.printAll();
  24.         db.closeDatabase();
  25.        
  26.     }
  27. }
  28. //////////////////////////////////////////////////////////////////////////////////
  29. /*
  30.  * To change this template, choose Tools | Templates
  31.  * and open the template in the editor.
  32.  */
  33. package trabfinal;
  34.  
  35. /**
  36.  *
  37.  * @author pablo
  38.  */
  39. public class Book {
  40.  
  41.     String author;
  42.     String title;
  43.     String ISBN;
  44.     String box;
  45.     String area;
  46.     String publisher;
  47.  
  48.     public Book(String author, String title, String ISBN, String box, String area, String publisher) {
  49.         this.author = author;
  50.         this.title = title;
  51.         this.ISBN = ISBN;
  52.         this.box = box;
  53.         this.area = area;
  54.         this.publisher = publisher;
  55.     }
  56.  
  57.     public String getAuthor() {
  58.         return author;
  59.     }
  60.  
  61.     public String getTitle() {
  62.         return title;
  63.     }
  64.  
  65.     public String getISBN() {
  66.         return ISBN;
  67.     }
  68.  
  69.     public String getBox() {
  70.         return box;
  71.     }
  72.  
  73.     public String getArea() {
  74.         return area;
  75.     }
  76.  
  77.     public String getPublisher() {
  78.         return publisher;
  79.     }
  80.  
  81.     @Override
  82.     public String toString() {
  83.         return "Author: " + author + "\nTitle: " + title + "\nISBN: " + ISBN + "\nBox: " + box + "\nArea: " + area + "\nPublisher: " + publisher;
  84.     }
  85. }
  86. ////////////////////////////////////////////////////////////////////////
  87. /*
  88.  * To change this template, choose Tools | Templates
  89.  * and open the template in the editor.
  90.  */
  91. package trabfinal;
  92.  
  93. import com.db4o.Db4oEmbedded;
  94. import com.db4o.ObjectContainer;
  95. import com.db4o.ObjectSet;
  96.  
  97. /**
  98.  *
  99.  * @author pablo
  100.  */
  101. public class Database {
  102.  
  103.     public static ObjectContainer db;
  104.  
  105.     public Database() {
  106.         db = Db4oEmbedded.openFile("database4.db");
  107.     }
  108.  
  109.     public void insertBook(Book b) {
  110.         db.store(b); //store data
  111.         db.commit(); //update database
  112.     }
  113.  
  114.     public void removeBook(Book b) {
  115.         db.delete(b); //remove data
  116.         db.commit(); //remove database
  117.     }
  118.  
  119.     public ObjectSet searchBook(Object dado) {
  120.         return db.queryByExample(dado);
  121.     }
  122.    
  123.     public void printAll(){
  124.         ObjectSet<Book> query = db.query(Book.class);
  125.        
  126.         for(Book b : query){
  127.             System.out.println(b);
  128.         }
  129.     }
  130.    
  131.     public void closeDatabase(){
  132.         db.close();
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment