Created
September 28, 2020 18:40
-
-
Save DungGramer/c94e162ec2bcce59afb5fdc69cdf70ca to your computer and use it in GitHub Desktop.
Quản lý sinh viên bằng db4o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Bai1; | |
import com.db4o.ObjectContainer; | |
import com.db4o.ObjectSet; | |
import com.db4odoc.f1.chapter1.Pilot; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Book { | |
private String name; | |
private String author; | |
private String description; | |
private int year; | |
public Book() { | |
} | |
public Book(String name) { | |
this.name = name; | |
} | |
public Book(String name, String author, String description, int year) { | |
this.name = name; | |
this.author = author; | |
this.description = description; | |
this.year = year; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public int getYear() { | |
return year; | |
} | |
public void setYear(int year) { | |
this.year = year; | |
} | |
public void addBook(ObjectContainer db) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.printf("Nhap ten: "); | |
setName(scanner.nextLine()); | |
System.out.printf("Nhap tac gia: "); | |
setAuthor(scanner.nextLine()); | |
System.out.printf("Nhap tom tat: "); | |
setDescription(scanner.nextLine()); | |
System.out.printf("Nhap nam xuat ban: "); | |
setYear(scanner.nextInt()); | |
Book book = new Book(this.name, this.author, this.description, this.year); | |
db.store(book); | |
} | |
public static void listResult(List<?> result) { | |
System.out.println(result.size()); | |
for (Object o : result) { | |
System.out.println(o); | |
} | |
} | |
public void getAllDb(ObjectContainer db) { | |
ObjectSet objectSet = db.queryByExample(Book.class); | |
Book.listResult(objectSet); | |
} | |
public void updateDb(ObjectContainer db) { | |
Book book = new Book(); | |
Scanner scanner = new Scanner(System.in); | |
System.out.printf("Nhap ten can sua: "); | |
String find = scanner.nextLine(); | |
ObjectSet result = db.queryByExample(new Book(find)); | |
Book found = (Book) result.next(); | |
int key; | |
System.out.printf("Nhap muc muon sua: " + | |
"\n1. Ten" + | |
"\n2. Tac gia" + | |
"\n3. Mo ta" + | |
"\n4. Nam" + | |
"\n> "); | |
key = scanner.nextInt(); | |
switch (key) { | |
case 1: | |
System.out.println("Nhap lai ten: "); | |
String newName; | |
newName = scanner.nextLine(); | |
newName = scanner.next(); | |
found.setName(newName); | |
break; | |
case 2: | |
System.out.printf("Nhap lai tac gia: "); | |
String newAuthor = scanner.nextLine(); | |
found.setAuthor(newAuthor); | |
break; | |
case 3: | |
System.out.printf("Nhap lai mo ta: "); | |
String newDescription = scanner.nextLine(); | |
found.setDescription(newDescription); | |
break; | |
case 4: | |
System.out.printf("Nhap lai nam xuat ban: "); | |
int newYear = scanner.nextInt(); | |
found.setYear(newYear); | |
break; | |
} | |
db.store(found); | |
} | |
public void deteleDb(ObjectContainer db) { | |
Book book = new Book(); | |
Scanner scanner = new Scanner(System.in); | |
System.out.printf("Nhap ten sach can xoa: "); | |
String find = scanner.nextLine(); | |
ObjectSet result = db.queryByExample(new Book(find)); | |
Book found = (Book) result.next(); | |
db.delete(found); | |
} | |
@Override | |
public String toString() { | |
return "Ten: " + name + ", Tac gia: " + author + ", Mo ta: " + description + ", Nam xuat ban: " + year; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Bai1; | |
import com.db4o.Db4oEmbedded; | |
import com.db4o.ObjectContainer; | |
import java.io.File; | |
public class Main { | |
final static String url = "./book.db4o"; | |
public static void main(String[] args) { | |
// new File(url).delete(); | |
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), url); | |
Book book = new Book(); | |
/* book.addBook(db); | |
book.getAllDb(db); | |
db.close();*/ | |
Menu menu = new Menu(db); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment