文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

利用java和sqlserver建立简易图书管理系统的完整步骤

2024-04-02 19:55

关注

1.所需功能:

游览所有图书、查找书籍、建立新图书,借出和归还图书,删除某一图书

2.流程:

数据库连接

3.环境及其语言:

Idea,JDK13,驱动包Microsoft JDBC Driver 8.2.2 for SQL Server

4.数据库建立

5.操作:

数据库连接:sql.T3.Test1.java

官网下载驱动包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);

选择需要的操作:sql.T3.Test1.java

1.游览所有图书

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);
}

2.查找书籍

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_h2 = sc.nextInt();
    f_l = true;
    switch (b_h2){
    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);
}

3.建立新图书

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);
}

4借出图书

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_h2 = sc.nextInt();
    f_l = true;
    switch (b_h2){
    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_h2 = sc.nextInt();
        if (b_h2 == 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);
}

5. 归还图书删除某一图书

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);
}

6.删除某一图书

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);
}

释放连接: sql.T1.JDBCUtil.java

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建立简易图书管理系统的文章就介绍到这了,更多相关java和sqlserver图书管理系统内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯