圖書管理系統(JDBC)


public class BookSystem{   //圖書系統管理類

 private Scanner sc =new Scanner(System.in);

public static void main(String[] args){

           BookSystem bs = new BookSystem();

           bs.start();

}

public void start(){

while (true){

System.out.println("請輸入要執行的命令");
System.out.println("-----------------------------------------------------");
System.out.println("---------------------圖書管理系統---------------------");
System.out.println(" 1.增加圖書 2.查找圖書");
System.out.println(" 3.修改圖書 4.刪除圖書");
System.out.println(" 5.列出所有圖書 6.退出系統");
System.out.println("----------------------------------------------------");
int command = sc.nextInt();

if (command==1){
insertNewBook();
}else if (command==2){
updateBook();

}else if (command==3){
deleteBook();

}else if (command==4){
selectBook();

}else if (command==5){
listAll();
}else {
System.out.println("退出系統");
}
public void insertNewBook(){
System.out.println("請輸入書的名字");
String bookName = sc.next();
System.out.println("請輸入書的作者");
String author = sc.next();
System.out.println("請輸入書的ISBN");
String isbn = sc.next();

Book book = new Book();
book.setName(bookName);
book.setAuthor(author);
book.setIsbn(isbn);

//把book對象交給數據庫來處理
int result = BookDB.insertBook(book);
if (result>0){
System.out.println("添加成功");
}else {
System.out.println("添加失敗");
}
}

public void updateBook(){
System.out.println("請輸入修改的書名");
String bookname = sc.next();
Book book =new Book();
book.setName(bookname);
int result = BookDB.updateBook(book);
if(result>0){
System.out.println("修改成功");
}else{
System.out.println("修改失敗");
}
}
public void deleteBook(){
System.out.println("請輸入要刪除的書名");
String newbook = sc.next();
Book book =new Book();
book.setName(newbook);
int result = BookDB.deleteBook(book);
if (result>0){
System.out.println("刪除成功");
}else {
System.out.println("刪除失敗");
}
}

public void selectBook(){
Book book = BookDB.selectBook();
System.out.println(book);

}
public void listAll(){
List<Book> list = BookDB.listAll();
if (list.size() == 0){
System.out.println("暫無書籍信息");
System.out.println("是否要添加新的書籍:Y/N");
String input = sc.next();
if ("y".equals(input.toLowerCase())){
insertNewBook();
}
}
for (Book book : list) {
System.out.println(book.getBid()+":"+book.getName());
}
}

}
public class BookDB { //專門處理圖書管理,數據部分

public static int insertBook(Book book) {
String sql = "insert into book(name,author,isbn) values (?,?,?)";
return DBUtils.update(sql,book.getName(),book.getAuthor(),book.getIsbn());
}

public static List<Book> listAll(){
Connection conn = DBUtils.getConn();
String sql ="select * from book";
PreparedStatement stmt=null;
ResultSet rs=null;

List<Book> list = new ArrayList<>();
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()){
Book book = new Book();
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setIsbn(rs.getString("isbn"));
book.setBid(rs.getInt("id"));
list.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,rs);
}
return list;
}

public static int updateBook(Book book){
String sql ="update book set name=? where name=? ";
System.out.println("請輸入修改的書名");
Scanner sc = new Scanner(System.in);
return DBUtils.update(sql,book.getName(),sc.next());
}

public static Book selectBook(){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
conn = DBUtils.getConn();
String sql ="select * from book where name=?";
try {
stmt = conn.prepareStatement(sql);
Scanner sc =new Scanner(System.in);
System.out.println("請輸入查找的書名");
stmt.setString(1,sc.next());
rs = stmt.executeQuery();
while (rs.next()){
Book book1 = new Book();
book1.setName(rs.getString("name"));
book1.setAuthor(rs.getString("author"));
book1.setIsbn(rs.getString("isbn"));
book1.setBid(rs.getInt("id"));
return book1;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,rs);
}
return null;
}

public static int deleteBook(Book book){
String sql ="delete from book where name =?";
System.out.println("請輸入刪除的書名");
Scanner sc = new Scanner(System.in);
return DBUtils.update(sql,sc.next());
}

}
public class DBUtils {// 工具類
public static void closeAll(Connection conn, PreparedStatement stmt, ResultSet rs) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 創建一個數據庫連接(封裝方法)
public static Connection getConn() {
// url規定數據庫連接的類型,地址,端口,和數據庫名,后面可以接參數
String url = "jdbc:mysql://localhost:3306/j0302?useSSL=true";
String username = "root";
String password = "123456";
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);
Connection conn =
DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("驅動未找到");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("數據庫連接失敗");
}
}
// 不定長參數
public static int update(String sql, Object... obArray){
Connection conn = DBUtils.getConn();
PreparedStatement stmt=null;
try {
stmt = conn.prepareStatement(sql);
for (int i = 0; i < obArray.length; i++) {
stmt.setObject(i+1,obArray[i]);
}
int result = stmt.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(conn,stmt,null);
}
// 如果代碼執行到這個位置,那么肯定是出了異常
// 這個時候直接返回0;
return 0;
}
}
 
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM