学习Java-结束

在三周的学习中,我完成了Java编程的学习,并在最后一天进行了答辩。我利用所学知识开发了一个图书管理系统,包括用户和图书的数据设计,创建了books和users两个数据库表,实现了Dao、Model、Util和服务层的Java代码,并且应用了JDBC进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习了三周,终于到了最后一天,今天是答辩时间。
结合所学知识做出一个项目,我做了一个图书管理系统。
1.数据
用户:用户编号,用户名,密码
图书:图书ISBN,图书名称,图书作者,图书价格,借书人,借书电话。
2.数据库的建立
a.books图书信息管理表
b.users用户信息表
3.主要Java代码分析
a.Dao类(图书Dao类为例)

public class BookDao implements IBook {
     DBUtil db=new DBUtil();

    @Override
     public void insertBook(Book newBook) throws Exception {
         //增加借阅图书信息
         Connection conn=db.getConn();
         PreparedStatement pstm=null;
         String sql_insert="insert into books values(?,?,?,?,?,?,?,?)";

         pstm=conn.prepareStatement(sql_insert);

         pstm.setInt(1,newBook.getId());
         pstm.setString(2,newBook.getISBN());
         pstm.setString(3,newBook.getBookName());
         pstm.setDouble(4,newBook.getPrice());
         pstm.setString(5,newBook.getAuthor());
         pstm.setString(6,newBook.getPublish());
         pstm.setString(7,newBook.getBorrowerName());
         pstm.setString(8,newBook.getBorrowerPhone());

         int row=pstm.executeUpdate();
         System.out.println("添加成功"+row+"行收到影响");
         db.closeConn(null,pstm,conn);

     }

    // 根据编号删除信息记录
    @Override
    public  void deleteBook(int id) throws Exception {
        Scanner input=new Scanner(System.in);
        UserDao userDao=new UserDao();
        BookService bookService=new BookService();
        System.out.println("请输入您的id,我们需要检测你的身份");
        int in=input.nextInt();
        int fn=userDao.findtype(in);
         Connection conn=db.getConn();
         PreparedStatement pstm=null;

         if(fn==0){
             String sql_delete="delete from books where id=?";
             pstm=conn.prepareStatement(sql_delete);
             pstm.setInt(1,id);
             int row=pstm.executeUpdate();
             if (row!=0){
                 System.out.println("删除成功,"+row+"行收到影响");
             }
         }else {
             System.out.println("\n您不是管理员,您没有权限删除用户信息,抱歉!\n我们将自动为您返回菜单\n");
             bookService.do_getMenu();
         }

        db.closeConn(null,pstm,conn);
    }

    @Override
    public void updateBook(int id, Book modBook) throws Exception {
        //根据编号修改借阅图书信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update books set ISBN=?, bookname=?, price=?, author=?,publish=?,borrowername=?, borrowerphone=? where id=?";

        pstm = conn.prepareStatement(sql_update);
        pstm.setString(1,modBook.getISBN());
        pstm.setString(2,modBook.getBookName());
        pstm.setDouble(3,modBook.getPrice());
        pstm.setString(4,modBook.getAuthor());
        pstm.setString(5,modBook.getPublish());
        pstm.setString(6,modBook.getBorrowerName());
        pstm.setString(7,modBook.getBorrowerPhone());
        pstm.setInt(8,id);
        int row = pstm.executeUpdate();
        System.out.println("修改成功,"+row+"行受到影响");

        db.closeConn(null, pstm, conn);
    }
    @Override
    public void selectByID(int id) throws Exception {
        //根据学生编号查询图书信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById = "select * from books where id=?";

        pstm = conn.prepareStatement(sql_selectById);
        pstm.setInt(1, id);

        rs = pstm.executeQuery();
        if (rs.next()){
            System.out.println("查询成功,信息如下:");
            System.out.println("学生编号\t\t"+ "图书ISBN\t\t" + "图书名称\t\t" + "书的价格\t\t"+ "图书作者\t\t"+ "图书出版社\t\t"+ "借书人的姓名\t\t\t"+ "借书人的电话\t\t");

                System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("ISBN")+"\t\t\t"+rs.getString("BookName")+"\t\t\t"+rs.getFloat("Price")+"\t\t"+rs.getString("Author")+"\t\t"+rs.getString("publish")+"\t\t"+rs.getString("BorrowerName")+"\t\t"+rs.getString("BorrowerPhone"));

        }else{
            System.out.println("没有此同学的借阅信息");
        }

        db.closeConn(rs, pstm, conn);
    }

    @Override
    public void selectAllBook() throws Exception {

    }


    public void selectALLBook() throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from books";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("所有借阅信息如下:");
        System.out.println("学生编号\t\t"+ "图书ISBN\t\t\t" + "图书名称\t\t\t" + "书的价格\t\t\t"+ "图书作者\t\t\t"+ "图书出版社\t\t"+ "借书人的姓名\t\t"+ "借书人的电话\t\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("ISBN")+"\t\t\t"+rs.getString("BookName")+"\t\t\t"+rs.getFloat("Price")+"\t\t\t"+rs.getString("Author")+"\t\t\t"+rs.getString("publish")+"\t\t\t"+rs.getString("BorrowerName")+"\t\t\t"+rs.getString("BorrowerPhone"));
        }
        db.closeConn(rs, pstm, conn);
    }
}

b.Model类(BookModel为例)

public class Book {
    private  int id;
    private  String ISBN;
    private  String bookName;
    private  double price;
    private  String author;
    private  String publish;
    private  String borrowerName ;
    private  String borrowerPhone;

    public Book() {
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", ISBN='" + ISBN + '\'' +
                ", bookName='" + bookName + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                ", publish='" + publish + '\'' +
                ", borrowerName='" + borrowerName + '\'' +
                ", borrowerPhone='" + borrowerPhone + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish;
    }

    public String getBorrowerName() {
        return borrowerName;
    }

    public void setBorrowerName(String borrowerName) {
        this.borrowerName = borrowerName;
    }

    public String getBorrowerPhone() {
        return borrowerPhone;
    }

    public void setBorrowerPhone(String borrowerPhone) {
        this.borrowerPhone = borrowerPhone;
    }

    public Book(int id, String ISBN, String bookName, double price, String author, String publish, String borrowerName, String borrowerPhone) {
        this.id = id;
        this.ISBN = ISBN;
        this.bookName = bookName;
        this.price = price;
        this.author = author;
        this.publish = publish;
        this.borrowerName = borrowerName;
        this.borrowerPhone = borrowerPhone;
    }
}

c.Util类

public class BookUtil {
    Scanner input = new Scanner(System.in);

    //提供book参数:书
    public Book getBook(String str) {
        Book newBook = new Book();

        System.out.println("请输入学生的编号:");
        newBook.setId(input.nextInt());
        System.out.println("请输入"+str+"书的ISBN:");
        newBook.setISBN(input.next());
        System.out.println("请输入"+str+"书的名称:");
        newBook.setBookName(input.next());
        System.out.println("请输入"+str+"书的价格:");
        newBook.setPrice(input.nextDouble());
        System.out.println("请输入"+str+"书的作者:");
        newBook.setAuthor(input.next());
        System.out.println("请输入"+str+"书的出版社:");
        newBook.setPublish(input.next());
        System.out.println("请输入"+str+"借书人的名称");
        newBook.setBorrowerName(input.next());
        System.out.println("请输入"+str+"借书人的电话");
        newBook.setBorrowerPhone(input.next());


        return newBook;
    }
    //2. 提供id参数: 供查、删除、修改使用
    public int getId(String str) {
        System.out.println("请输入需要" + str + "的学生编号:");
        int id = input.nextInt();
        return id;
    }
}

d.service

public class BookService {
    public Scanner input = new Scanner(System.in);

//1. 实现系统登录功能
    public void do_Login() throws Exception {
        System.out.print("请输入用户名:");
        String uname=input.next();
        System.out.print("请输入密码:");
        String upwd=input.next();
        System.out.print("请输入职业(管理员(0):学生(1)):");
        int utype=input.nextInt();

        boolean b = new UserDao().getHaveUser(uname, upwd, utype);
        if (b){
            System.out.println("登录成功");
            System.out.println("\t欢迎使用宁财图书管理系统");
                new BookService().do_getMenu();
        }else{
            System.out.println("没有此用户!请先注册");
            new BookService().do_register();
            System.exit(0);
        }
    }
    //注册
         public void do_register() throws Exception {
        Scanner input = new Scanner(System.in);
        System.out.println("\t--------------登录失败-------------" );
        System.out.println("\t1.注册\t2.退出系统" );
        int b=input.nextInt();
        if (b==1){
            System.out.print("请输入用户名:" );
            String usewrname=input.next();
            System.out.print("请输入密码:" );
            String password1=input.next();
            System.out.print("请确认密码:" );
            String password2=input.next();
            System.out.print("请输入你的类型:\nps:\t管理员(0)\t学生(1)");
            int type=input.nextInt();
            if(password1.equals(password2)&&(type==0||type==1)){
                UserDao.register(usewrname,password1,type);
                System.out.println("登入还是退出Y/N");
                String dl = input.next();
                if (dl.equals("N")) {
                    System.exit(0);
                } else if (!dl.equals("Y")) {
                    System.out.println("请输入Y或N!");
                    System.exit(0);
                }else{
                    do_getMenu();
                }
            }else {
                System.out.println("密码或角色有误,请重新注册!");
            }
        }else {
            System.exit(0);
        }
    }


 
    //实现管理功能

    public void do_getMenu() throws Exception {
        boolean flag=true;
        while(flag){
            int id= 0;

            System.out.println("1.图书管理\t2.用户管理\t3.退出系统");

            System.out.println("请选择序号(1-2):");
            int choice = input.nextInt();
            if (choice == 1){
                while(flag){
                    System.out.println("1.添加图书信息\t2.删除图书信息\t3.修改图书信息\t4.查询单个图书信息\t\t5.查询全部图书信息\t\t6.返回上一级");
                    System.out.println("请选择序号(1-6):");

                    choice = input.nextInt();

                    switch (choice){
                        case 1:
                            Book newBook = new BookUtil().getBook("添加");

                            new BookDao().insertBook(newBook);
                            break;
                        case 2:
                            id=new BookUtil().getId("删除");
                            new BookDao().deleteBook(id);
                            break;
                        case 3:
                            id=new BookUtil().getId("修改");
                            Book modBook=new BookUtil().getBook("修改后");
                            new BookDao().updateBook(id,modBook);
                            break;
                        case 4:
                            id=new BookUtil().getId("查找");
                            new BookDao().selectByID(id);
                            break;
                        case 5:
                            new BookDao().selectALLBook();
                            break;
                        case 6:
                            new BookService().do_getMenu();
                            break;
                    }
                }
            }
            else if (choice == 2){
                while(flag){
                    System.out.println("1.增加用户\t2.删除用户\t3.修改用户\t4.查找用户\t5.查找全部用户\t6.返回上级");
                    System.out.print("请选择序号(1-6):");

                    choice = input.nextInt();

                    switch(choice){
                        case 1:
                            User newUser = new UserUtil().getUser("添加");
                            new UserDao().insertUser(newUser);
                            break;
                        case 2:
                            id = new UserUtil().getId("删除");

                            new UserDao().deleteUser(id);
                            break;
                        case 3:
                            id = new UserUtil().getId("修改");
                            User modUser = new UserUtil().getUser("修改后");
                            new UserDao().updateUser(id, modUser);
                            break;
                        case 4:
                            id = new UserUtil().getId("查询");
                            new UserDao().selectByID(id);
                            break;
                        case 5:
                            new UserDao().selectAllUser();
                            break;
                        case 6:
                            new BookService().do_getMenu();
                            break;

                    }
                }
        }
        else if (choice == 3){
                System.exit(0);//退出系统
            }

        }
    }

4.JDBC的建立

public class DBUtil {
    public String url = "jdbc:mysql:///db_book";
    public String username = "root";
    public String password = "123456";

    /**
     * 1. 实现数据库连接的方法
     */
    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url, username, password);
            System.out.println("连接数据库成功.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 2. 释放数据库连接
     */
    public void closeConn(ResultSet rs, PreparedStatement pstm, Connection conn) throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值