您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用jdbc如何實現一個圖書館借閱系統,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
簡介
圖書館管理系統分為用戶登錄和管理員登錄,整個系統的控制是在控制臺下進行操作的。
用戶層
用戶可以進行注冊,登錄,借書,查書,密碼修改,還書等功能。
管理員層
管理員可以進行對管理書籍包括新書上架,修改庫存信息等功能。
系統本身
系統本身可以根據用戶是否逾期進行鎖定禁止借書的功能等。
需要掌握的java知識
java基礎語法,容器(list),jdbc,sql語句。
po層代碼
/** * * 對數據庫的操作進行單獨處理,更多的邏輯更改在更高的邏輯層編寫。降低耦合 * */ public class LibararyDao { /** * 注冊圖書 * @param user 注冊圖書信息 * @return */ public boolean addBook(Bookinfo bookinfo){ try { Connection con =JDBCutil.creatConnection(); String sql = "insert into bookinfo (bookname,zuozhe,shuliang,shengyushuliang,date)" + " values (?,?,?,?,?) "; PreparedStatement pre =con.prepareStatement(sql); pre.setString(1, bookinfo.getBookname()); pre.setString(2, bookinfo.getZuozhe()); pre.setInt(3, bookinfo.getShuliang()); pre.setInt(4, bookinfo.getShuliang()); pre.setDate(5, new java.sql.Date(bookinfo.getData().getTime())); pre.execute(); } catch (SQLException e) { System.out.println("圖書注冊失敗"); e.printStackTrace(); return false; } return true; } /** * 注冊賬號 * @param user 注冊信息 * @return */ public boolean addUser(User user){ try { Connection con =JDBCutil.creatConnection(); String sql = "insert into userinfo (username,password,books) values (?,?,0) "; PreparedStatement pre =con.prepareStatement(sql); pre.setString(1, user.getUsername()); pre.setString(2, user.getPassword()); pre.execute(); } catch (SQLException e) { System.out.println("注冊失敗"); e.printStackTrace(); return false; } return true; } /** * 查詢用戶 * @param user 信息 * @return */ public ResultSet queryUser(User user){ try { Connection con =JDBCutil.creatConnection(); String sql = "select * from userinfo where username = ? "; PreparedStatement pre; pre = con.prepareStatement(sql); pre.setString(1, user.getUsername()); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("查詢失敗"); e.printStackTrace(); return null; } } /** * 更新持有書 * @param user 信息 * @return */ public boolean updateUserbooks(User user){ try { Connection con =JDBCutil.creatConnection(); String sql = "update userinfo set books = ? where username = ? "; PreparedStatement pre; pre = con.prepareStatement(sql); pre.setInt(1, user.getBooks()); pre.setString(2, user.getUsername()); int r =pre.executeUpdate(); if(r>0){ return true; }else{ return false; } } catch (SQLException e) { System.out.println("查詢失敗"); e.printStackTrace(); return false; } } /** * 借書表中插入 * @param username 借書人 * @param bookname 書名 * @return */ public boolean addborrowinfo(String username,String bookname){ try { Connection con =JDBCutil.creatConnection(); String sql = "insert into borrowinfo (username,bookname,borrowdate,returndate)" + " values (?,?,?,?) "; PreparedStatement pre; pre = con.prepareStatement(sql); pre.setString(1, username); pre.setString(2, bookname); pre.setDate(3, new java.sql.Date(new Date().getTime())); pre.setDate(4, new java.sql.Date(DateUtil.addMonth(new Date()).getTime())); pre.execute(); } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return false; } return true; } /** * 查書 * @param bookname 書名 * @return */ public ResultSet queryBook(String bookname){ try { Connection con =JDBCutil.creatConnection(); String sql = "select * from bookinfo where bookname ='"+bookname+"'"; PreparedStatement pre; pre = con.prepareStatement(sql); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return null; }} /** * 查詢所有的書籍 * @return */ public ResultSet queryAllBook(){ try { Connection con =JDBCutil.creatConnection(); String sql = "select * from bookinfo "; PreparedStatement pre; pre = con.prepareStatement(sql); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return null; } } /** * 分頁 * @param page * @return */ public ResultSet querySubBookInfo(int page){ try { Connection con =JDBCutil.creatConnection(); PreparedStatement pre; String sql = "select * from bookinfo limit ?,5 "; pre = con.prepareStatement(sql); pre.setInt(1, page); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return null; } } /** * 數據頁數 五條數據為一組 * @return */ public int queryPageInfo(){ try { int zongshu = 0; Connection con =JDBCutil.creatConnection(); String sql1 = "select count(id) as cou from bookinfo "; Statement sta =con.createStatement(); ResultSet res =sta.executeQuery(sql1); if(res.next()) { zongshu = res.getInt("cou"); if(zongshu == 0)return 0; } int a =0; if(zongshu%5==0){ a = zongshu/5; }else{ a = zongshu/5+1; } return a; } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return -1; } } /** * 更新剩余數量 * @param bookname 書名 * @param sysl 數量 * @return */ public boolean updateBookinfo(String bookname,int sysl){ try { Connection con =JDBCutil.creatConnection(); String sql = "update bookinfo set shengyushuliang = ? where bookname = ?"; PreparedStatement pre; pre = con.prepareStatement(sql); pre.setInt(1,sysl); pre.setString(2, bookname); int r =pre.executeUpdate(); if(r>0){ return true; }else{ return false; } } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return false; } } /*public boolean queryBook(String...strings){ try { Connection con =JDBCutil.creatConnection(); String sql = "select bookname from bookinfo where 1=1 "; PreparedStatement pre; pre = con.prepareStatement(sql); ResultSet r =pre.executeQuery(); if(r.next()){ return true; }else{ return false; } } catch (SQLException e) { System.out.println("借書失敗"); e.printStackTrace(); return false; }*/ /** * 查詢用戶的所有的借閱信息 * @param username * @return */ public ResultSet queryBorrowinfo(String username){ try { Connection con =JDBCutil.creatConnection(); String sql = "select * from borrowinfo where username = '"+username+"'"; PreparedStatement pre = con.prepareStatement(sql); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("查詢失敗"); e.printStackTrace(); return null; } } /** * 查詢借閱信息 * @param username * @return */ public ResultSet queryBorrowinfo(String username,String bookname){ try { Connection con =JDBCutil.creatConnection(); String sql = "select * from borrowinfo where username = ? and bookname = ?"; PreparedStatement pre = con.prepareStatement(sql); pre.setString(1, username); pre.setString(2, bookname); ResultSet r =pre.executeQuery(); return r; } catch (SQLException e) { System.out.println("查詢失敗"); e.printStackTrace(); return null; } } /** * 刪除借書 * @param username 人名 * @param bookname 書名 * @return */ public boolean deleteBorrowInfo(String username,String bookname){ try { Connection con =JDBCutil.creatConnection(); String sql = "delete from borrowinfo where username = ? and bookname = ? "; PreparedStatement pre = con.prepareStatement(sql); pre.setString(1, username); pre.setString(2, bookname); pre.execute(); return true; } catch (SQLException e) { System.out.println("查詢失敗"); e.printStackTrace(); return false; } } }
control層代碼
/** * asdfasd 發斯蒂芬 * */ package library.controller; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import library.dao.LibararyDao; import library.po.Bookinfo; import library.po.BorrowInfo; import library.po.User; public class BookController { private static LibararyDao bookdao; private BorrowInfo b; public BookController() { if(bookdao == null) bookdao = new LibararyDao(); } public static void main(String[] args) { //System.out.println(new BookController().addUser(new User("123", "dvdf"))); //System.out.println(new BookController().borrowBook("123", new User("123", "dvdf"))); // new BookController().fenyeInfo(2); } /** * 用戶登錄 * @param user 傳過來的賬號密碼 * @return 返回1為登陸成功 2為不存在 3異常 */ public int loginUser(User user){ ResultSet rs =bookdao.queryUser(user);//驗證用戶是否存在 try { if(rs!=null&&rs.next()){ boolean b =rs.getString("password").equals(user.getPassword()); if(b){ return 1; } } return 2; } catch (SQLException e) { e.printStackTrace(); return 3; } } /** * 管理員登陸 * @param user 傳過來的賬號密碼 * @return 返回1為登陸成功 2為不存在 3異常 */ public int loginGuanli(User user){ ResultSet rs =bookdao.queryUser(user);//驗證管理員是否存在 try { if(rs!=null&&rs.next()){ boolean b =rs.getString("password").equals(user.getPassword()); int b1 = rs.getInt("isguanli");//管理員標志位1 if(b&&b1==1){ return 1; } }return 2; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 3; } } /** * 查詢書籍列表 * @return 返回1為查詢成功并已經輸出 2為失敗或其他 */ public int queryBookinfo(){ try { ResultSet r =bookdao.queryAllBook(); while(r!=null&&r.next()){ System.out.println("書名"+r.getString("bookname")+"作者"+ r.getString("zuozhe")+"總數"+r.getInt("shuliang")+"剩余數量"+r.getInt("shengyushuliang")); } if(r==null)return 2; return 1; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 2; } } /** * 查詢書籍數量和剩余數量 * @return 返回1為查詢成功并已經輸出 2為失敗或其他 */ public ResultSet queryBookK(String bookname){ ResultSet r =bookdao.queryBook(bookname); return r; } /** * 查詢書籍 * @return 返回1為查詢成功并已經輸出 2為失敗或其他 */ public int queryBook(String bookname){ try { ResultSet r =bookdao.queryBook(bookname); while(r!=null&&r.next()){ System.out.println("書名"+r.getString("bookname")+"作者"+ r.getString("zuozhe")+"總數"+r.getInt("shuliang")+"剩余數量"+r.getInt("shengyushuliang")); } if(r==null)return 2; return 1; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 2; } } /** * 加入新的圖書信息 * @param bookinfo 書籍的詳細信息 * @return 1成功 2失敗 */ public int addBookinfo(Bookinfo bookinfo){ boolean b = bookdao.addBook(bookinfo); if(b)return 1; return 2; } /** * 注冊用戶 * @param user * @return */ public int addUser(User user){ ResultSet rs =bookdao.queryUser(user); try { if(rs!=null&&!rs.next()){ boolean b =bookdao.addUser(user); if(b)return 1; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 3; } return 2; } /** * 查詢用戶所有信息 * @param user * @return */ public int queryUser(User user){ ResultSet r = bookdao.queryUser(user); try { if(r.next()){ user.setBooks(r.getInt("books")); System.out.println(user); return 1; } return 2; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 2; } } /** * 借書 先檢查書是否存在,再檢查書籍是否有庫存,然后檢查是否已結借此書 * 最后以上檢查沒有錯誤,就可以借書。 用戶持有數量增加,借書信息詳解錄入,庫存更新 * * 注意事項 * 1 resultset 默認指向數據前一個位置 必須 用 next * 2 需要加入事務 防止出現錯誤造成數據表更新不一致 * @param bookname 書名 * @return */ public String borrowBook(String bookname,User user){ try { if(!bookdao.queryBook(bookname).next()){ return "借書失敗-書籍不存在";} ResultSet querybookrs = bookdao.queryBook(bookname); while(querybookrs.next()){ int t = querybookrs.getInt("shengyushuliang"); if(t<1){return "庫存不足";} ResultSet rs =bookdao.queryBorrowinfo(user.getUsername()); if(rs!=null&&!rs.next()){ //加入借閱詳細信息 boolean b1 =bookdao.addborrowinfo(user.getUsername(), bookname); //更新借書數量 ResultSet rs1 = bookdao.queryUser(user); rs1.next(); user.setBooks(rs1.getInt("books")+1); bookdao.updateUserbooks(user); //更新庫存 boolean b2 =bookdao.updateBookinfo(bookname, t-1); //System.out.println(b1+" "+b2); return b1?(b2?"借書成功":"借書失敗"):("借書失敗"); } } return null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return "借書失敗-其他錯誤"; } } /**還書 * 更新用戶持有數,歸還書的庫存,刪除借書信息表中信息 * @param user 更新數據數 * @param bookinfo 更新庫存后的數 * @return */ public int returnBook(User user,String bookname){ boolean b1 = bookdao.deleteBorrowInfo(user.getUsername(),bookname); boolean b2 = false; boolean b3 = false; try { //更新庫存 ResultSet r = bookdao.queryBook(bookname); if(r.next()){ int i = r.getInt("shengyushuliang"); b2 = bookdao.updateBookinfo(bookname,i+1); } //更新持有書數 r = bookdao.queryUser(user); if(r.next()){ int i = r.getInt("books"); user.setBooks(i-1); b3 =bookdao.updateUserbooks(user); } if(!b1||!b2||!b3){ return 2; } return 1; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 2; } } /** * 查詢用戶的借書情況 * @param user 用戶 * @return 返回借閱信息 */ public List<BorrowInfo> queryborrowinfo(User user){ ResultSet r = bookdao.queryBorrowinfo(user.getUsername()); List<BorrowInfo> l = new ArrayList<BorrowInfo>(); try { while(r!=null&&r.next()){ b = new BorrowInfo(); b.setUsername(r.getString("username")); b.setBookname(r.getString("bookname")); b.setBorrowdate(r.getDate("borrowdate")); b.setReturndate(r.getDate("returndate")); l.add(b); } return l; } catch (SQLException e) { e.printStackTrace(); return null; } } /** * 指定頁碼 * @param age */ public void fenyeInfo(int age){ int i =bookdao.queryPageInfo(); System.out.println("總頁數:"+i); try { if(i>0){ ResultSet r = bookdao.querySubBookInfo((age-1)*5); while(r!=null&&r.next()){ System.out.println("書名"+r.getString("bookname")+"作者"+ r.getString("zuozhe")+"總數"+r.getInt("shuliang")+"剩余數量"+r.getInt("shengyushuliang")); } } } catch (SQLException e) { e.printStackTrace(); } } /** * 指定上下頁 * @param age */ public void fenyeInfo(String age){ } /** * 更新圖書信息 * 更新作者,庫存,剩余數量,不更新日期,書名(不就是添加了新的書籍了嗎) * 可更新任務可以多選 * * 書名進行判斷是否存在 * 庫存和剩余數量進行邏輯上的驗證 */ public void updateBookinfo(String bookname){ Scanner scan = new Scanner(System.in); System.out.println("更新作者輸入:1,跳過11"); int i =scan.nextInt(); List<List<String>> list = new ArrayList<List<String>>(); List<String> list1 = null; if(i==1){ String newbookname = scan.next(); list1 = new ArrayList<String>(); list1.add("zuozhe"); list1.add("'"+newbookname+"'"); list.add(list1); } //如果更新了庫存,剩余數量也會跟著更新 需要判斷庫存邏輯的正確性 System.out.println("更新數量輸入2,跳過22"); int ii = scan.nextInt(); if(ii==2){ System.out.println("請更新庫存數量"); int newsum = scan.nextInt(); //判斷數量邏輯的正確性 ResultSet r = queryBookK(bookname); int oldsum=0;int sykc=0;//舊庫存和剩余數量 try { while(r.next()){ oldsum = r.getInt("shuliang"); sykc = r.getInt("shengyushuliang"); } if(newsum>oldsum){ sykc+=newsum-oldsum;//記錄更新后的剩余數量 //System.out.println("根據已有庫存,庫存總量為---"+newsum); } else if(newsum<oldsum&&newsum>=sykc){//如何新的庫存少于就庫存。判斷新庫存與剩余數量的關系 sykc-=newsum; }else{ System.out.println("輸入錯誤"); return ; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } list1 = new ArrayList<String>(); list1.add("shuliang"); list1.add(String.valueOf(newsum)); list.add(list1); list1 = new ArrayList<String>(); list1.add("shengyushuliang"); list1.add(String.valueOf(sykc)); list.add(list1); } bookdao.updateBookinfo(list, bookname); } }
view層
package library.view; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; import library.controller.BookController; import library.po.Bookinfo; import library.po.BorrowInfo; import library.po.User; /** * 視圖類,也是界面的顯示層。程序運行的入口 * 想說的話: * 如果使用web作為v層,只需要把bookrun方法拆開就可以了。對應會話跟蹤,把 private static User user 存入session對象就可以了 * 框架的話用springmvc 。對于dao加入orm框架(hibernate ,mybatis)。整合起來也就是常見的ssm體系或ssh體系 * 數據庫的選擇更是隨你心情。mysql SQLserver 等等。 * 項目bug仔細看的話很多。本意想給入門的同學一個思路方向。對于一些變量的命名請不要學我(偷懶),養成一個良好的 * 習慣。祝你在編程的道路上越走越遠!! */ public class View { //controller 業務控制類 private static BookController bookc = new BookController(); //登陸后的用戶跟蹤類似session private static User user; //控制主界面的循環 private static boolean flag = true; //接受控制臺輸入 private static Scanner scan = new Scanner(System.in); public static void main(String[] args) { bookRun(); } //主界面 應該考慮其他輸入的限定 (當輸入字符時,系統停止運行) public static void bookRun(){ try{ do{ System.out.println("歡迎來到圖書借閱系統\n登陸輸入: 1\n注冊輸入: 2\n管理員管理:3"); int i1 = scan.nextInt(); switch (i1) { case 1://登陸邏輯 System.out.println("輸入賬號"); String username = scan.next(); System.out.println("密碼"); String password = scan.next(); int i =bookc.loginUser(new User(username, password)); if(i==1){ System.out.println("登陸成功"); user = new User(username, password); loginSuccess(user); }else if(i!=1){ System.out.println("登陸失敗,檢查密碼或者賬號"); } break; case 2: //注冊邏輯 可以添加驗證密碼 進行密碼的確定 java api有把明文進行處理的方法,請自行搜索 //還可以加入正則表達式進行賬號的限定(對于特殊字符輸入的處理等) System.out.println("輸入申請賬號"); String username1 = scan.next(); System.out.println("密碼"); String password1 = scan.next(); //此處可以再次確認密碼的邏輯 System.out.println(addUser(new User(username1, password1))); System.out.println("返回上一層"); break; case 3://管理員邏輯 System.out.println("輸入管理員賬號"); String username2 = scan.next(); System.out.println("密碼"); String password2 = scan.next(); user = new User(username2, password2); int i2 =bookc.loginGuanli(user); if(i2==1){ loginGuanliSuccess(); }else{ System.out.println("登陸失敗,檢查密碼或者賬號"); } break; default : System.out.println("輸入序號不正確"); break; } }while(flag); }catch(InputMismatchException e){ System.out.println("輸入異常,請重啟系統"); } } //此處可以添加新的功能而不影響已有功能 (可以改為switch語句) private static void loginGuanliSuccess() { while(true){ System.out.println("添加圖書:1\n更改圖書信息:2\n返回:3"); int i = scan.nextInt(); if(i==1){ addBook(); }else if(i==2){ updateBookinfo(); }else if(i==3){ return; } } } //更新圖書 /** * 更新圖書 只有管理員權限可以操作 */ private static void updateBookinfo() { //應該判斷書名是否存在 contro層設計 System.out.println("請輸入書名"); String bookname = scan.next(); int a = bookc.queryBook(bookname); if(a!=1){ System.out.println("不存在,已返回上一層"); return; } /* System.out.println("更新作者輸入1,跳過11"); int i =scan.nextInt(); List<List<String>> list = new ArrayList<List<String>>(); List<String> list1 = null; if(i==1){ String newbookname = scan.next(); list1 = new ArrayList<String>(); list1.add("zuozhe"); list1.add("'"+newbookname+"'"); list.add(list1); } //如果更新了庫存,剩余數量也會跟著更新 需要判斷庫存邏輯的正確性 System.out.println("更新數量輸入2,跳過22"); int ii = scan.nextInt(); if(ii==2){ //判斷數量邏輯的正確性 ResultSet r =bookc.queryBookK(bookname); try { int i1=0;int i2=0; while(r.next()){ i1 = r.getInt("shuliang"); i2 = r.getInt("shengyushuliang"); } if(ii>i1){ i2+=ii-i1; } if(ii<i1&&ii>=i2){ } System.out.println("更新后的數量錯誤"); return; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String newkucun= scan.next(); list1 = new ArrayList<String>(); list1.add("shuliang"); list1.add(newkucun); list.add(list1); }*/ bookc.updateBookinfo(bookname); } //添加圖書 需要保證名字唯一性 private static void addBook() { System.out.println("圖名"); String bookname = scan.next(); System.out.println("作者"); String zuozhe = scan.next(); System.out.println("數量"); int shuliang = scan.nextInt(); Bookinfo bookinfo = new Bookinfo(bookname, zuozhe, shuliang, shuliang,new Date()); int i =bookc.addBookinfo(bookinfo); if(i==1){ System.out.println("添加成功"); }else{ System.out.println("錯誤"); } } //注冊界面的實現 private static String addUser(User user){ switch (bookc.addUser(user)) { case 1: return "注冊成功"; case 2: return "用戶已注冊"; case 3: return "其他錯誤,重試"; } return "其他錯誤,重試"; } //登陸功能實現 private static void loginSuccess(User user ){ while (true){ System.out.println("查看借閱信息請輸入:1,借書:2,還書:3,賬號信息:4,退出:5" + "查詢圖書:6修改密碼:7\n"); int i = scan.nextInt(); switch (i) { case 1: borrowInfo(user); break; case 2: borrowBook(user); break; case 3: returnBook(user); break; case 4: queryUser(user); break; case 5: return ; case 6: System.out.println("請輸入書名"); queryBook(scan.next()); break; case 7: break; default: System.out.println("輸入錯誤"); break; } } } //查詢借閱信息 private static void borrowInfo(User user ){ List<BorrowInfo> l=bookc.queryborrowinfo(user); if(l==null||l.isEmpty()){ System.out.println("沒有借閱信息"); return; } for (BorrowInfo borrowInfo : l) { System.out.println(borrowInfo); } } //借書 private static void borrowBook(User user){ System.out.println("請輸入借閱的書籍名稱"); String bookname =scan.next(); String flog = bookc.borrowBook(bookname, user); System.out.println(flog); System.out.println("是否繼續借書:y|n"); String flog1 = scan.next(); if(flog1.equalsIgnoreCase("y")){ borrowBook(user); } } //還書 private static void returnBook(User user1 ){ List<BorrowInfo> l=bookc.queryborrowinfo(user1); if(l==null||l.isEmpty()){ System.out.println("沒有借閱信息"); return; } for (BorrowInfo borrowInfo : l) { System.out.println(borrowInfo); } System.out.println("請輸入要還書籍的名字"); String bookname = scan.next(); int flog =bookc.returnBook(user1, bookname); if(flog==1){ System.out.println("還書成功"); }else{ System.out.println("失敗"); } System.out.println("是否繼續還書:y|n"); String flog1 = scan.next(); if(flog1.equalsIgnoreCase("y")){ returnBook(user1); } } //用戶信息 private static void queryUser(User user){ int i =bookc.queryUser(user); if( i!=1){ System.out.println("失敗"); } } private static void queryBook(String bookname){ int i = bookc.queryBook(bookname); if(i!=1)System.out.println("書籍不存在"); } private static void GuanliFace(){ while(true){ System.out.println("查詢書籍列表請輸入 :1;添加圖書:2;使用分頁查看書籍列表"); int i =scan.nextInt(); switch (i) { case 1: int i1= bookc.queryBookinfo(); if(i1==2)System.out.println("錯誤"); break; case 2: System.out.println("書名"); String booknname1 = scan.next(); System.out.println("作者"); String zuozhe1 = scan.next(); System.out.println("數量"); int shuliang1 = scan.nextInt(); Date date = new Date(); int i3 = bookc.addBookinfo(new Bookinfo(booknname1, zuozhe1, shuliang1, shuliang1, date)); if(i3==2)System.out.println("錯誤"); break; case 3: default: System.out.println("錯"); break; } } } private static void fenyeInfo(){ } }
數據庫
/用戶表 CREATE TABLE `userinfo` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(20) COLLATE utf8_bin DEFAULT NULL, `password` varchar(20) COLLATE utf8_bin DEFAULT NULL, `books` int(10) DEFAULT NULL, `isguanli` int(10) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; /借書信息 CREATE TABLE `borrowinfo` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `bookname` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `borrowdate` date DEFAULT NULL, `returndate` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /書籍信息 字段用的拼音:-D CREATE TABLE `bookinfo` ( `id` int(10) NOT NULL AUTO_INCREMENT, `bookname` varchar(20) COLLATE utf8_unicode_ci NOT NULL, `zuozhe` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `shuliang` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `shengyushuliang` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `date` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
上述內容就是使用jdbc如何實現一個圖書館借閱系統,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。