您好,登录后才能下订单哦!
本文小编为大家详细介绍“如何利用java和sqlserver建立简易图书管理系统”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何利用java和sqlserver建立简易图书管理系统”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
游览所有图书、查找书籍、建立新图书,借出和归还图书,删除某一图书
数据库连接
选择需要的操作
释放连接
Idea,JDK13,驱动包Microsoft JDBC Driver 8.2.2 for SQL Server
数据库:sqlserver2019
Database:library
Table:Book
Book:书籍编号,书籍名称,书籍作者,库存量
管理员名:sa
密码:123456
官网下载驱动包Microsoft JDBC Driver 8.2.2 for SQL Server并加载到程序中。
//注册驱动 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("书籍导入:请依次输入书籍的编号、书名、作者和导入数量:"); 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.运行结果演示
sqlserver中的部分数据:
idea中的演示结果:
显示书籍:
查找书籍及借出:
归还书籍及删除书籍:
读到这里,这篇“如何利用java和sqlserver建立简易图书管理系统”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。