书籍管理系统

书籍管理系统


项目描述
实现对数据库中的书籍进行增、删、改、查操作。其中涉及到MVC,jsp,servlet等。
项目代码

项目中是在web下开发,所使用的IDE为eclipse,Tomcat和jdk版本均为8.0

1.由于要连接数据库,所以新建工具类包即基本数据库连接类,切记导入数据库驱动连接包并放置在工程目录WebContent/WEB-INF/lib下。
  温馨提示:类编写完成后可以进行本地数据库连接测试,确保数据库连接成功。

package cn.mxf.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//数据库连接类
public class BaseDao {
   
    //声明Connection连接类
    private Connection conn;
    //创建PreparedStatement对象,用来执行SQL语句
    private PreparedStatement ps;
    //用来存放数据的结果集
    private ResultSet rs;

    // 连接数据库
    public void getConnection() {
        // 加载驱动程序
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/booksys?characterEncoding=utf-8";
            //通过getConnection方法连接数据库
            conn = DriverManager.getConnection(url, "root", "");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 关闭数据库  从后往前依次关闭
    public void close() {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 实现对数据的更新--增加,删除,修改操作;其中Object...为可变参数
    public int executeUpdate(String sql, Object... objects) {
        try {
            this.getConnection();
            ps = conn.prepareStatement(sql);
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i + 1, objects[i]);
                }
                return ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        return -1;
    }

    // 实现对数据的查询操作
    public ResultSet executeQuery(String sql, Object... objects) {
        try {
            this.getConnection();
            ps = conn.prepareStatement(sql);
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i + 1, objects[i]);
                }
                return rs = ps.executeQuery();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
2.由于我们需要做的是对书籍的增删改查等操作,所以新建书籍实体类包里面包含数据实体类和书籍分类类;同时在mysql数据库中新建book(书籍信息)和category(书籍分类)表。
注意:其中book表的id设置为自动递增

书籍类

package cn.mxf.entity;

import java.util.Date;

//书籍类
public class Book {
   
        // 对所有的属性生成get和set方法
    private int id;// 书籍编号
    private String name;// 书籍名称
    private double price;// 价格
    private String author;// 作者
    private Date pubDate;// 出版日期
    private int categoryId;// 书籍分类
        // 无参构造方法
    public Book() {
    }
        // 不带id的有参构造方法
    public Book(String name, double price, String author, Date pubDate, int categoryId) {
        super();
        this.name = name;
        this.price = price;
        this.author = author;
        this.pubDate = pubDate;
        this.categoryId = categoryId;
    }
        // 带id的有参构造方法
    public Book(int id, String name, double price, String author, Date pubDate, int categoryId) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.author = author;
        this.pubDate = pubDate;
        this.categoryId = categoryId;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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 Date getPubDate() {
        return pubDate;
    }

    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

}

书籍分类类

package cn.mxf.entity;

public class Category {
   
    // 书籍分类编号
    private int id;
    // 书籍对应分类名
    private String name;
    // 生成带参构造方法
    public Category(int id, String name) {
        super();
        this.id = id;
        
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值