您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“如何利用java和sqlserver建立簡易圖書管理系統(tǒng)”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“如何利用java和sqlserver建立簡易圖書管理系統(tǒng)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
游覽所有圖書、查找書籍、建立新圖書,借出和歸還圖書,刪除某一圖書
數(shù)據(jù)庫連接
選擇需要的操作
釋放連接
Idea,JDK13,驅(qū)動包Microsoft JDBC Driver 8.2.2 for SQL Server
數(shù)據(jù)庫:sqlserver2019
Database:library
Table:Book
Book:書籍編號,書籍名稱,書籍作者,庫存量
管理員名:sa
密碼:123456
官網(wǎng)下載驅(qū)動包Microsoft JDBC Driver 8.2.2 for SQL Server并加載到程序中。
//注冊驅(qū)動 DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); //建立連接 String dbUL="jdbc:sqlserver://localhost:1433;DatabaseName=Library"; String Name="sa"; String Paw="123456"; conn=DriverManager.getConnection(dbUL,Name,Paw);
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); sql = "select * from Book"; rs = st.executeQuery(sql); while (rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String aut = rs.getString("aut"); int num = rs.getInt("num"); System.out.println("id=" + id + " name=" + name + " aut:" + aut + " num:" + num); } }catch (SQLException e){ e.printStackTrace(); }finally{ JDBCUtil.release(conn, st, rs); }
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); System.out.println("查找方式:1.按編號查找;2.按書名查找;3.按作者查找;4.放棄操作;"); b_h3 = sc.nextInt(); f_l = true; switch (b_h3){ case 1: System.out.println("輸入書籍編號:"); book_num = sc.nextInt(); sql1 = "select * from Book where id=" + book_num; break; case 2: System.out.println("輸入書籍名稱:"); book_name = sc.next(); sql1 = "select * from Book where name='" + book_name + "'"; break; case 3: System.out.println("輸入書籍作者:"); author = sc.next(); sql1 = "select * from Book where id='" + author + "'"; break; case 4: f_l = false; break; } if (f_l){ sql = sql1; rs = st.executeQuery(sql); while (rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String aut = rs.getString("aut"); int num = rs.getInt("num"); System.out.println("id=" + id + " name=" + name + " aut:" + aut + " num:" + num); } } else System.out.println("對象已放棄操作"); }catch (SQLException e){ e.printStackTrace(); }finally{ JDBCUtil.release(conn, st, rs); }
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); System.out.println("書籍導入:請依次輸入書籍的編號、書名、作者和導入數(shù)量:"); book_id = sc.nextInt(); book_name = sc.next(); author = sc.next(); book_num = sc.nextInt(); sql1 = "insert into Book values(" + book_id + ",'" + book_name + "','" + author + "'," + book_num + ")"; st.executeUpdate(sql1); }catch (SQLException e){ e.printStackTrace(); }finally{ JDBCUtil.release(conn, st); }
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); System.out.println("查找方式:1.按編號查找;2.按書名查找;3.按作者查找;4.放棄操作;"); b_h3 = sc.nextInt(); f_l = true; switch (b_h3){ case 1: System.out.println("輸入書籍編號:"); book_num = sc.nextInt(); sql1 = "select * from Book where id=" + book_num; break; case 2: System.out.println("輸入書籍名稱:"); book_name = sc.next(); sql1 = "select * from Book where name='" + book_name + "'"; break; case 3: System.out.println("輸入書籍作者:"); author = sc.next(); sql1 = "select * from Book where id='" + author + "'"; break; case 4: f_l = false; break; } if (f_l){ sql = sql1; rs = st.executeQuery(sql); while (rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String aut = rs.getString("aut"); int num = rs.getInt("num"); System.out.println("id=" + id + " name=" + name + " aut:" + aut + " num:" + num); b_h = num; book_id = id; } System.out.println("確認借出:確認:1,取消:2"); b_h3 = sc.nextInt(); if (b_h3 == 1){ if (b_h == 0) System.out.println("館藏書目為0,無法借出"); else{ b_h--; sql1 = "UPDATE Book SET num=" + b_h + "WHERE id =" + book_id; st.executeUpdate(sql1); } } else System.out.println("你已取消操作"); } else System.out.println("對象已放棄操作"); }catch (SQLException e) { e.printStackTrace(); }finally{ JDBCUtil.release(conn, st, rs); }
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); System.out.println("輸入歸還書id:"); book_id = sc.nextInt(); sql = "select * from Book where id=" + book_id; rs = st.executeQuery(sql); while (rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String aut = rs.getString("aut"); int num = rs.getInt("num"); System.out.println("id=" + id + " name=" + name + " aut:" + aut + " num:" + num); b_h = num; } b_h++; sql1 = "UPDATE Book SET num=" + b_h + "WHERE id =" + book_id; st.executeUpdate(sql1); }catch (SQLException e){ e.printStackTrace(); }finally{ JDBCUtil.release(conn, st, rs); }
try{ DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); conn = DriverManager.getConnection(dbUL, Name, Paw); st = conn.createStatement(); System.out.println("輸入刪除書id:"); book_id = sc.nextInt(); sql1 = "delete from Book where id=" + book_id; st.executeUpdate(sql1); }catch (SQLException e){ e.printStackTrace(); }finally{ JDBCUtil.release(conn, st, rs); }
public static void release(Connection conn, Statement st, ResultSet rs)//當rs不為空時 { closeRs(rs); closeSt(st); closeConn(conn); } public static void release(Connection conn, Statement st)// 當rs為空時 { closeSt(st); closeConn(conn); }
6.運行結(jié)果演示
sqlserver中的部分數(shù)據(jù):
idea中的演示結(jié)果:
顯示書籍:
查找書籍及借出:
歸還書籍及刪除書籍:
讀到這里,這篇“如何利用java和sqlserver建立簡易圖書管理系統(tǒng)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。