JDBC编写简单的图书管理系统,实现增删改查操作

有错误的地方希望指正

注:我用NetBeans写的,连接mysql需要在NetBeans里引入jar包

一.数据库连接,Navicat创建book表

在Navicat里新建连接,连接Mysql,

在Navicat中创建school数据库,在school数据库里创建表book,设置字段bid(图书编号),bname(图书名),bprice(图书价格),类型分别为整型,varchar类型,及浮点型

并在其中添加初始数

二.创建Book类

在bookdao包下创建Book类,定义变量bid,bname,bprice,创建有参无参构造方法,以及get set函数,并封装字段

package bookdao;

/**
 *
 * @author www
 */
public class Book {
     private int bid;
     private String bname;
     private double bprice;

    public Book() {
    }

    public Book(int bid, String bname, double bprice) {
        this.bid = bid;
        this.bname = bname;
        this.bprice = bprice;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public double getBprice() {
        return bprice;
    }

    public void setBprice(double bprice) {
        this.bprice = bprice;
    }
}

三.创建BookDao类

在bookdao包下创建BookDao类,类中含有对图书实现增删改查以及根据图书编号查找图书的功能的方法

bookdao包下

public class BookDao{
      Connection conn;//
      PreparedStatement st;
      ResultSet rs;

    public BookDao(Connection conn, PreparedStatement st, ResultSet rs) {
        this.conn = conn;
        this.st = st;
        this.rs = rs;
    }

 查找所有书籍的方法:

//查找所有图书数据
   public  void query()throws SQLException{
       //创建图书集合存储图书数据
       List<Book> list=new ArrayList<>();
       st=conn.prepareStatement("select *from Book");
       rs=st.executeQuery();
       
       while(rs.next()){
           //创建实体类对象
           Book b=new Book();
           b.setBid(rs.getInt(1));
           b.setBname(rs.getString(2));
           b.setBprice(rs.getDouble(3)); 
           //将实体类对象添加到集合中,集合中就有实体类对象所有数据
           list.add(b);
          System.out.println("bid:"+b.getBid()+"bname:"+b.getBname()+"bprice:"+b.getBprice());
       }               
    }

这里加入一个exit方法,根据输入的编号判断所要查找或删除的书籍是否存在

//判断书籍是否存在
    public boolean exit(Book b,int bid)throws SQLException{//参数为Bookl类和输入的书籍序号                        
         st=conn.prepareStatement("select * from Book where bid=?");      
         st.setInt(1,bid);       
         rs=st.executeQuery();//返回数据集对象
          if(rs.next()){
           return true; 
          }//若存在,返回结果
          else{
          return false;
          }//若不存在,返回false
}

增加书籍的方法:这里需要用exit方法根据编号判断要添加的书籍是否存在,结合while循环语句,若要添加的书籍不存在,则直接添加,添加成功后用return让程序不再执行;若书籍存在则重新输入编号,继续判断

//增加图书
  public void doAdd(Book b)throws SQLException{
       while(true){
     
         Scanner sc=new Scanner(System.in);
         System.out.println("请输入你需要添加的书籍序号");
         int id=sc.nextInt();        
        if(exit(b,id)==false){//要添加的书籍序号不存在,可以继续添加
          System.out.println("请输入你需要添加的书籍名");
          String bname=sc.next();
          System.out.println("请输入你需要添加的书籍价格");
          double bprice=sc.nextDouble();
          st= conn.prepareStatement("insert into Book(bid,bname,bprice)values(?,?,?)");
         st.setInt(1, id);
         st.setString(2, bname);
         st.setDouble(3,bprice);
         st.executeUpdate();
         System.out.println("添加成功!");
         return;
        }else{//书籍存在
             System.out.println("您想要添加的书籍已存在,请重新添加");
        }
   
       }           
    }

更新图书的方法:要判断书籍是否存在,存在才能更新

//更新图书
  public void doUpdate(Book b) throws SQLException{
       while(true){
      
        System.out.println("请输入你需要修改的书籍序号");
        Scanner sc=new Scanner(System.in);
        int id=sc.nextInt();
        
         if(exit(b,id)==true){
           System.out.println("请输入新书籍名");
           String bname=sc.next();  //sc.nextLine()
           System.out.println("请输入新书籍价格");
           double bprice=sc.nextDouble();
           st=conn.prepareStatement("update Book set bname=?,bprice=? where bid=? ");//  此处将序号修改为原来的当作未修改序号,改进能否不修改学号
                                  
                  st.setString(1, bname);
                  st.setDouble(2, bprice);
                  st.setInt(3, id);//数字表示sql语句中第几个变量
                   st.executeUpdate();
                   System.out.println("更新成功");
                   return;
         }else{
             System.out.println("您输入的图书序号有误,请重新输入");
         }
       
       }
       

删除图书的方法:

//删除图书
  public void doRemove(Book b) throws SQLException{
       while(true){
       
              
        System.out.println("请输入你需要删除的书籍的序号");
        Scanner sc=new Scanner(System.in);
        int id=sc.nextInt();
        
        if(exit(b,id)==true){
            st=conn.prepareStatement("delete from Book where bid=?");
            st.setInt(1, id);
            System.out.println("删除成功!");
             st.executeUpdate();
            return;
        }else{
             System.out.println("您输入的书籍序号不存在,请重新输入");
        }
        
       }
}

根据编号查找图书的方法:

//根据序号查找图书
   public void doFindBid(Book b)throws SQLException{
       while(true){        
        System.out.println("请输入你需要查找的书籍的序号");
        Scanner sc=new Scanner(System.in);
        int bid=sc.nextInt();        
         if(exit(b,bid)){//书籍存在
              st=conn.prepareStatement("select * from Book where bid=?");
              st.setInt(1,bid);
              rs=st.executeQuery();
              while(rs.next()){
                  int id=bid;
                  String name=rs.getString(2);
                  Double price=rs.getDouble(3);
                   System.out.println("bid:"+id+"bname:"+name+"bprice:"+price);
                   return;
              }
         }else{
             System.out.println("您输入的图书序号有误,请重新输入");
         }
       }   
}

四.主类

package BookT;

import bookdao.Book;
import bookdao.BookDao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 *
 * @author 空
 */
public class BookTest {
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException{
    Connection conn=null;
    PreparedStatement st=null;
    ResultSet rs=null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    //建立连接
     conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8", "root", "123456");
     //创建Book 和BookDao的对象
    Book b=new Book();
    
    BookDao bookdao=new BookDao(conn,st,rs);
      while(true){     
      System.out.println("---------欢迎来到图书管理系统----------");
      System.out.println("该系统能实现以下功能");
      System.out.println("1.查找所有书籍");
      System.out.println("2.增加书籍");
      System.out.println("3.修改书籍");
      System.out.println("4.删除书籍");
      System.out.println("5.根据序号查找书籍");
       System.out.println("6.退出");
      Scanner sc=new Scanner(System.in);
      System.out.println("请输入您需要的功能:");
          int s=sc.nextInt();
      switch(s){
          case 1:
             
              bookdao.query();
               break;
          case 2:
              
              bookdao.doAdd(b);
              break;
          case 3:
            
             bookdao.doUpdate(b);
              break;
          case 4:
          
             bookdao.doRemove(b);
              break;
          case 5:
           
              bookdao.doFindBid(b);
               break;
           case 6:  
                System.out.println("退出成功!");
                 
               return;
        
      }
      }
    }catch (ClassNotFoundException e) {
           e.printStackTrace();
        }
    catch(SQLException e){          
                e.printStackTrace();
     }finally{
        if(rs!=null&&st!=null){
          rs.close();
          st.close(); 
        }
         else if(conn!=null)
          conn.close();
        }   
  }
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值