By GokiSoft.com|
16:27 21/07/2023|
Java Advanced
Bài tập - CRUD Quản lý sách - Kết nối CSDL - Lập trình Java nâng cao
Thiết kế database đặt tên library gồm các bảng sau
- Book: gồm các column -> id tự tăng, bookName, price, authorName
Viết menu chương trình sau
1. Thêm 1 sách vào CSDL
2. Sửa 1 sách theo id trong CSDL
3. Hiển thị toàn bộ sách trong CSDL
4. Xoá sách trong CSDL
5. Thoát
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
GokiSoft.com
2023-02-01 08:03:36
#Book.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.overview.lesson04;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Book {
int id;
String bookName, authorName;
float price;
public Book() {
}
public Book(int id, String bookName, String authorName, float price) {
this.id = id;
this.bookName = bookName;
this.authorName = authorName;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten sach: ");
bookName = scan.nextLine();
System.out.println("Nhap gia: ");
price = Float.parseFloat(scan.nextLine());
System.out.println("Nhap tac gia: ");
authorName = scan.nextLine();
}
@Override
public String toString() {
return "id=" + id + ", bookName=" + bookName + ", authorName=" + authorName + ", price=" + price;
}
}
#BookDAO.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.overview.lesson04;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java2.lesson07.StudentDAO;
/**
*
* @author diepvan
*/
public class BookDAO {
static Connection con = null;
static PreparedStatement statement = null;
private static void openConnection() {
// TODO add your handling code here:
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/C2109I", "root", "");
} catch (SQLException e) {
}
}
private static void closeConnection() {
//B3. Dong ket noi
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(StudentDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
statement = null;
con = null;
}
public static List<Book> list() {
List<Book> dataList = new ArrayList<>();
openConnection();
String sql = "select * from books";
try {
statement = con.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book book = new Book(
resultSet.getInt("id"),
resultSet.getString("bookname"),
resultSet.getString("author_name"),
resultSet.getFloat("price")
);
dataList.add(book);
}
} catch (SQLException ex) {
Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
return dataList;
}
public static Book find(int id) {
Book book = null;
openConnection();
String sql = "select * from books where id = ?";
try {
statement = con.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
book = new Book(
resultSet.getInt("id"),
resultSet.getString("bookname"),
resultSet.getString("author_name"),
resultSet.getFloat("price")
);
break;
}
} catch (SQLException ex) {
Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
return book;
}
public static void insert(Book book) {
openConnection();
try {
String sql = "insert into books(bookname, price, author_name) values (?,?,?)";
statement = con.prepareStatement(sql);
statement.setString(1, book.getBookName());
statement.setFloat(2, book.getPrice());
statement.setString(3, book.getAuthorName());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
}
public static void update(Book book) {
openConnection();
try {
String sql = "update books set bookname=?, price=?, author_name=? where id=?";
statement = con.prepareStatement(sql);
statement.setString(1, book.getBookName());
statement.setFloat(2, book.getPrice());
statement.setString(3, book.getAuthorName());
statement.setInt(4, book.getId());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
}
public static void delete(int id) {
openConnection();
try {
String sql = "delete from books where id=?";
statement = con.prepareStatement(sql);
statement.setInt(1, id);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex);
}
closeConnection();
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.overview.lesson04;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
static List<Book> dataList = new ArrayList<>();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
list();
break;
case 2:
insert();
break;
case 3:
update();
break;
case 4:
delete();
break;
case 5:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while(choose != 5);
}
static void showMenu() {
System.out.println("1. Hien thi danh sach");
System.out.println("2. Them");
System.out.println("3. Sua");
System.out.println("4. Xoa");
System.out.println("5. Thoat");
System.out.println("Chon: ");
}
private static void list() {
dataList = BookDAO.list();
System.out.println("Danh sach sach: ");
for (Book book : dataList) {
System.out.println(book);
}
}
private static void insert() {
Book book = new Book();
book.input();
BookDAO.insert(book);
list();
}
private static void update() {
System.out.println("Nhap id can sua: ");
int id = Integer.parseInt(scan.nextLine());
Book book = BookDAO.find(id);
if(book == null) {
System.out.println("Khong tim thay sach");
} else {
book.input();
BookDAO.update(book);
}
}
private static void delete() {
System.out.println("Nhap id sach can xoa: ");
int id = Integer.parseInt(scan.nextLine());
BookDAO.delete(id);
list();
}
}
Hieu Ngo
2021-09-16 04:25:18
#BaseDAO.java
import java.sql.*;
public class BaseDAO {
static Connection conn = null;
static PreparedStatement statement = null;
static void openConnection() {
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME,Config.PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
static void closeConnection(){
if(statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
#Book.java
import java.util.Scanner;
public class Book {
int id, price;
String bookName, authorName;
public Book() {
}
public Book(int id, int price, String bookName, String authorName) {
this.id = id;
this.price = price;
this.bookName = bookName;
this.authorName = authorName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public Scanner getScanner() {
return new Scanner(System.in);
}
public void input() {
System.out.println("Nhap ten sach");
this.bookName = getScanner().nextLine();
System.out.println("Nhap price");
this.price = getScanner().nextInt();
System.out.println("Nhap ten tac gia:");
this.authorName = getScanner().nextLine();
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", price=" + price +
", bookName='" + bookName + '\'' +
", authorName='" + authorName + '\'' +
'}';
}
}
#BookDAO.java
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDAO extends BaseDAO {
public static List<Book> getBookList() {
openConnection();
List<Book> data = new ArrayList<>();
String sql = "select * from book";
try {
statement = conn.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book book = new Book(
resultSet.getInt("id"),
resultSet.getInt("price"),
resultSet.getString("bookName"),
resultSet.getString("authorName")
);
data.add(book);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
closeConnection();
return data;
}
public static void insert(Book book) {
openConnection();
try {
String sql = "insert into book(bookName,price,authorName)" + "values(?,?,?)";
statement = conn.prepareStatement(sql);
statement.setString(1,book.getBookName());
statement.setInt(2,book.getPrice());
statement.setString(3, book.getAuthorName());
statement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
closeConnection();
}
public static void update(Book book) {
openConnection();
String sql = "update book set bookName = ?, price = ?, authorName = ? where id = ?";
try {
statement = conn.prepareStatement(sql);
statement.setString(1,book.getBookName());
statement.setInt(2,book.getPrice());
statement.setString(3, book.getAuthorName());
statement.setInt(4,book.getId());
statement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
closeConnection();
}
public static void delete(int id) {
openConnection();
String sql = "delete from book where id = ?";
try {
statement = conn.prepareStatement(sql);
statement.setInt(1, id);
statement.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
closeConnection();
}
}
#Config.java
public interface Config {
String DATANAME = "library";
String DB_URL = "jdbc:mysql://localhost/" + DATANAME;
String USERNAME = "root";
String PASSWORD = "";
}
#Main.java
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String [] args) {
int choice;
Book book = new Book();
List<Book> data = BookDAO.getBookList();
do {
menu();
choice = (new Scanner(System.in)).nextInt();
switch (choice) {
case 1:
book.input();
BookDAO.insert(book);
break;
case 2:
System.out.println("Nhap id muon sua:");
int idUpdate = (new Scanner(System.in)).nextInt();
Book b = data.get(idUpdate-1);
b.input();
BookDAO.update(b);
System.out.println("Update thanh cong");
break;
case 3:
for(Book book1 : data) {
System.out.println(book1.toString());
}
break;
case 4:
System.out.println("Nhap id muon xoa");
int idDelete = (new Scanner(System.in)).nextInt();
BookDAO.delete(idDelete);
System.out.println("xoa thanh cong!");
break;
case 5:
return;
}
}while (choice>=1 && choice<=5);
}
static void menu() {
System.out.println("1. Thêm 1 sách vào CSDL");
System.out.println("2. Sửa 1 sách theo id trong CSDL");
System.out.println("3. Hiển thị toàn bộ sách trong CSDL");
System.out.println("4. Xoá sách trong CSDL");
System.out.println("5. Thoát");
System.out.println("So ban chon");
}
}
Võ Như Việt
2021-08-25 03:51:36
#Book.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2Buoi6;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class Book {
int id;
String bookName,authorName;
int price;
public Book() {
}
public Book(int id, String bookName, int price, String authorName) {
this.id = id;
this.price = price;
this.bookName = bookName;
this.authorName = authorName;
}
public Book(int price, String bookName, String authorName) {
this.price = price;
this.bookName = bookName;
this.authorName = authorName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public void input(){
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten sach: ");
bookName = scan.nextLine();
System.out.println("Nhap gia tien: ");
price = Utility.scanInt(scan);
System.out.println("Nhap ten tac gia: ");
authorName = scan.nextLine();
}
@Override
public String toString() {
return "Book{" + "id=" + id + ", price=" + price + ", bookName=" + bookName + ", authorName=" + authorName + '}';
}
public void display(){
System.out.println(this);
}
}
#BookCSDL.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2Buoi6;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author ADMIN
*/
public class BookCSDL {
public static void insert(Book book) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
String sql = "insert into book(bookName,price,authorName) value (?,?,?)";
statement = conn.prepareStatement(sql);
statement.setString(1, book.getBookName());
statement.setInt(2, book.getPrice());
statement.setString(3, book.getAuthorName());
statement.execute();
System.out.println("Insert thanh cong");
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void update(Book book) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
String sql = "update book set bookName = ? , price = ?, authorName = ? where id = ?";
statement = conn.prepareStatement(sql);
statement.setString(1, book.getBookName());
statement.setInt(2, book.getPrice());
statement.setString(3, book.getAuthorName());
statement.setInt(4, book.getId());
statement.execute();
System.out.println("update thanh cong");
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void delete(int id) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
String sql = "delete from book where id = ?";
statement = conn.prepareStatement(sql);
statement.setInt(1, id);
statement.execute();
System.out.println("delete thanh cong");
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static List<Book> getBookList() {
List<Book> booksList = new ArrayList<>();
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
String sql = "select * from book";
statement = conn.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Book b = new Book(
resultSet.getInt("id"),
resultSet.getString("bookName"),
resultSet.getInt("price"),
resultSet.getString("authorName")
);
booksList.add(b);
}
System.out.println("hien thi thanh cong");
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(BookCSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
return booksList;
}
}
}
#Config.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2Buoi6;
/**
*
* @author ADMIN
*/
public interface Config {
String DB_NAME = "library";
String DB_URL = "jdbc:mysql://localhost:3306/" + DB_NAME ;
String USERNAME = "root";
String PASSWORD = "";
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2Buoi6;
import java.util.List;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class Main {
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
Book book = new Book();
int choose;
do {
showMenu();
choose = Utility.scanInt(scan);
switch(choose){
case 1:
System.out.println("Nhap thong tin quyen sach can them: ");
book.input();
BookCSDL.insert(book);
break;
case 2:
System.out.println("Nhap ID can chinh sua: ");
int checkId = Utility.scanInt(scan);
book.setId(checkId);
System.out.println("Nhap thong tin quyen sach: ");
book.input();
BookCSDL.update(book);
break;
case 3:
System.out.println("Danh sach thong tin cac quyen sach");
List<Book> bookslist = BookCSDL.getBookList();
for (Book book1 : bookslist) {
book1.display();
}
break;
case 4:
System.out.println("Nhap ID quyen sach can xoa: ");
int delID = Utility.scanInt(scan);
BookCSDL.delete(delID);
break;
case 5:
System.out.println("Thoat chuong trinh....");
break;
default:System.out.println("Choose....");
break;
}
} while (choose != 5);
}
static void showMenu(){
System.out.println("1.Them 1 quyen sach vao CSDL: ");
System.out.println("2.Sua 1 quyen sach theo id trong CSDL: ");
System.out.println("3.Hien thi toan bo sach trong CSDL: ");
System.out.println("4.Xoa 1 quyen sach theo id trong CSDL: ");
System.out.println("5.Thoat!!!");
System.out.println("Lua Chon: ");
}
}
#Utility.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Java2Buoi6;
import java.util.Scanner;
/**
*
* @author ADMIN
*/
public class Utility {
public static int scanInt(Scanner scan){
int value;
while (true) {
try {
value = Integer.parseInt(scan.nextLine());
return value;
} catch (NumberFormatException e) {
System.out.println("Nhap sai, nhap lai....");
}
}
}
}
Đào Mạnh Dũng
2021-08-24 14:45:32
#Book.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2374;
import java.util.Scanner;
/**
*
* @author Đào Dũng
*/
public class Book {
public int id;
public String bookName;
public float price;
public String authorName;
public Book() {
}
public Book(int id, String bookName, float price, String authorName) {
this.id = id;
this.bookName = bookName;
this.price = price;
this.authorName = authorName;
}
public void input(){
Scanner scanner = new Scanner(System.in);
System.out.println("nhap bookName");
this.bookName = scanner.nextLine();
System.out.println("nhap price");
this.price = Float.parseFloat(scanner.nextLine());
System.out.println("nhap authorName");
this.authorName = scanner.nextLine();
}
void inputId() {
Scanner scanner = new Scanner(System.in);
System.out.println("nhap id");
this.id = Integer.parseInt(scanner.nextLine());
}
void inputFull() {
Scanner scanner = new Scanner(System.in);
System.out.println("nhap id");
this.id = Integer.parseInt(scanner.nextLine());
System.out.println("nhap bookName");
this.bookName = scanner.nextLine();
System.out.println("nhap price");
this.price = Float.parseFloat(scanner.nextLine());
System.out.println("nhap authorName");
this.authorName = scanner.nextLine();
}
@Override
public String toString() {
return "Book{" + "id=" + id + ", bookName=" + bookName + ", price=" + price + ", authorName=" + authorName + '}';
}
}
#BookDB.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2374;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Đào Dũng
*/
public class BookDB {
public static List<Book> Sql(Book book, String act) {
Connection conn = null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
String sql = null;
if (act.equals("insert")) {
sql = "insert into book(bookName, price, authorName) values (?, ?, ?)";
}
if (act.equals("Update")) {
sql = "update book set bookName = ?, price = ?, authorName = ? where id = ?";
}
if (act.equals("delete")) {
sql = "delete from book where id = ?";
}
if (act.equals("select")) {
sql = "select * from book";
}
statement = conn.prepareStatement(sql);
if (act.equals("insert") || act.equals("Update")) {
statement.setString(1, book.bookName);
statement.setFloat(2, book.price);
statement.setString(3, book.authorName);
}
if (act.equals("delete") || act.equals("Update")) {
statement.setInt(act.equals("delete")?1:4, book.id);
}
if (act.equals("select")) {
ResultSet resultSet = statement.executeQuery();
List<Book> list = new ArrayList<>();
while(resultSet.next()) {
list.add(new Book(
resultSet.getInt("id"),
resultSet.getString("bookName"),
resultSet.getFloat("price"),
resultSet.getString("authorName")
));
}
return list;
}
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
}
#Config.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2374;
/**
*
* @author Đào Dũng
*/
public interface Config {
String DB_NAME = "library";
String DB_URL = "jdbc:mysql://localhost:3306/" + DB_NAME;
String USERNAME = "root";
String PASSWORD = "";
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2374;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Đào Dũng
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
menu();
switch(Integer.parseInt(scanner.nextLine())){
case 1 ->{
Book book = new Book();
book.input();
BookDB.Sql(book,"insert");
}
case 2 ->{
Book book = new Book();
book.inputFull();
BookDB.Sql(book,"Update");
}
case 3 ->{
List<Book> list = BookDB.Sql(null,"select");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
case 4 ->{
Book book = new Book();
book.inputId();
BookDB.Sql(book,"delete");
}
case 5 ->{
System.exit(0);
}
}
}
}
private static void menu() {
System.out.print("1. Thêm 1 sách vào CSDL\n" +
"\n" +
"2. Sửa 1 sách theo id trong CSDL\n" +
"\n" +
"3. Hiển thị toàn bộ sách trong CSDL\n" +
"\n" +
"4. Xoá sách trong CSDL\n" +
"\n" +
"5. Thoát\n\nChon : ");
}
}