IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 2021-10-01图书管理系统(JAVA+Mysql) -> 正文阅读

[大数据]2021-10-01图书管理系统(JAVA+Mysql)

图书管理系统(JAVA+Mysql)

一.编译环境
1.编译器:eclipse,windowsbuilder插件
2.数据库:mysql5.7

二.部分代码展示
1.代码架构
在这里插入图片描述
2.实体类

package com.model;
/**
 * 图书实体
 * @author 86159
 *
 */
public class Book {
	private int id;//图书编号
	private	String bookName;//图书名
	private String author;//作者
	private String sex;//作者性别
	private Float price;//价格
	private String bookTypeName;//图书类别
	private String bookDesc;//图书描述
	private Integer bookTypeId;//图书类别编号
	
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

	public Book(int id, String bookName, String author, String sex, Float price, String bookDesc, Integer bookTypeId) {
		super();
		this.id = id;
		this.bookName = bookName;
		this.author = author;
		this.sex = sex;
		this.price = price;
		this.bookDesc = bookDesc;
		this.bookTypeId = bookTypeId;
	}



	public Book(String bookName, String author, Integer bookTypeId) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.bookTypeId = bookTypeId;
	}


	public Book(String bookName, String author, String bookTypeName) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.bookTypeName = bookTypeName;
	}


	public Book(String bookName, String author,String sex,  Float price,  String bookDesc,Integer bookTypeId) {
		super();
		this.bookName = bookName;
		this.sex = sex;
		this.author = author;
		this.price = price;
		this.bookTypeId = bookTypeId;
		this.bookDesc = bookDesc;
	}

	public int getId() {
		return id;
	}

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

	public String getBookName() {
		return bookName;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAuthor() {
		return author;
	}

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

	public Float getPrice() {
		return price;
	}

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

	public Integer getBookTypeId() {
		return bookTypeId;
	}

	public void setBookTypeId(Integer bookTypeId) {
		this.bookTypeId = bookTypeId;
	}

	public String getBookTypeName() {
		return bookTypeName;
	}

	public void setBookTypeName(String bookTypeName) {
		this.bookTypeName = bookTypeName;
	}

	public String getBookDesc() {
		return bookDesc;
	}

	public void setBookDesc(String bookDesc) {
		this.bookDesc = bookDesc;
	}
}

3.数据库连接工具类

package com.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 数据库工具类
 *
 */
	public class DbUtil {
//	连接数据库
	private String dbUrl = "jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf-8";
	private String dbUserName = "root";//用户名
	private String dbPassword = "123456";//密码
	private String jdbcName = "com.mysql.jdbc.Driver";//驱动名称
/**
 * 获取数据库连接
 * @return
 * @throws Exception
 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
		return con;
	}
/**
 * 关闭数据库连接
 * @param con
 * @throws Exception
 */
	public void closeCon(Connection con) throws Exception{
		if (con != null){
			con.close();
		}
	}
/**
 * 测试数据库是否连接成功
 * @param args
 */
	public static void main(String[] args) {
		DbUtil dbUtil = new DbUtil();
		try {
			dbUtil.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接失败");
		}
	}
}

4.图书数据库操作类

package com.dao;

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

import com.model.Book;
import com.util.StringUtil;

/**
 * BookDao类
 * @author 86159
 *
 */
public class BookDao {
/**
 * 添加图书
 * @param con
 * @param book
 * @return
 * @throws Exception
 */
	public int add(Connection con ,Book book)throws Exception{
		String sql = "insert into t_book values(null,?,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setString(5, book.getBookDesc());
		pstmt.setInt(6, book.getBookTypeId());
		return pstmt.executeUpdate();
	
	}
	public ResultSet list(Connection con,Book book) throws Exception{
		StringBuffer sb = new StringBuffer("select * from t_book b , t_bookType bt where b.bookTypeId=bt.id");
		if(!StringUtil.isEmpty(book.getBookName())){
			sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
		}
		if(!StringUtil.isEmpty(book.getAuthor())){
			sb.append(" and b.author like '%"+book.getAuthor()+"%'");
		}
		if (book.getBookTypeId()!=null&& book.getBookTypeId()!=-1){
			sb.append(" and b.bookTypeId="+book.getBookTypeId());
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString());
		return pstmt.executeQuery();
	}
	/**
	 * 图书信息删除
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id) throws Exception{
		String sql = "delete from t_book where id=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();	
	}
	/**
	 * 图书信息修改
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,Book book) throws Exception{
		String sql = "update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setString(2, book.getAuthor());
		pstmt.setString(3, book.getSex());
		pstmt.setFloat(4, book.getPrice());
		pstmt.setString(5, book.getBookDesc());
		pstmt.setInt(6, book.getBookTypeId());
		pstmt.setInt(7, book.getId());
		return pstmt.executeUpdate();
	}
	public boolean exitBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
		String sql = "select * from t_book where bookTypeId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookTypeId);
		ResultSet rs = pstmt.executeQuery();
		return rs.next();
	}
}

5.图书数据库增删改查

package com.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.dao.BookDao;
import com.dao.BookTypeDao;
import com.model.Book;
import com.model.BookType;
import com.util.DbUtil;
import com.util.StringUtil;

public class BookAddInterFrm extends JInternalFrame {
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private DbUtil dbUtil = new DbUtil();
	private BookTypeDao bookTypeDao = new BookTypeDao();
	private BookDao bookDao = new BookDao();
	JComboBox bookTypeJcb;
	JTextPane priseTxt;
	JTextPane bookNameTxt;
	JTextPane authorTxt;
	JTextArea bookDescTxt;
	JRadioButton manJrb;
	JRadioButton femaleJrb;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookAddInterFrm frame = new BookAddInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();	
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookAddInterFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("\u56FE\u4E66\u6DFB\u52A0");
		setBounds(100, 100, 592, 568);
		
		JLabel label = new JLabel("\u56FE\u4E66\u540D\u79F0");
		
		JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u4F5C\u8005");
		
		JLabel lblNewLabel_1 = new JLabel("\u4F5C\u8005\u6027\u522B");
		
		bookNameTxt = new JTextPane();
		
		authorTxt = new JTextPane();
		
		manJrb = new JRadioButton("\u7537");
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("\u5973");
		buttonGroup.add(femaleJrb);
		
		JLabel lblNewLabel_2 = new JLabel("\u56FE\u4E66\u4EF7\u683C");
		
		JLabel label_1 = new JLabel("\u56FE\u4E66\u63CF\u8FF0");
		
	    bookDescTxt = new JTextArea();
	    bookTypeJcb = new JComboBox();
		
		JLabel label_2 = new JLabel("\u56FE\u4E66\u7C7B\u522B");
		
		JButton btnNewButton = new JButton("\u91CD\u7F6E");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValue();
			}
		});
		btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/\u91CD\u7F6E.png")));
		
		JButton btnNewButton_1 = new JButton("\u6DFB\u52A0");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				引入图书添加
				bookAddActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/\u6DFB\u52A0.png")));
		
		priseTxt = new JTextPane();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(60)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(label_1)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(label)
									.addComponent(lblNewLabel_1))
								.addComponent(label_2))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
									.addComponent(bookDescTxt, Alignment.LEADING)
									.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
											.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
											.addGroup(groupLayout.createSequentialGroup()
												.addComponent(manJrb)
												.addGap(18)
												.addComponent(femaleJrb)))
										.addGap(41)
										.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
											.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
												.addComponent(lblNewLabel_2)
												.addGap(18)
												.addComponent(priseTxt))
											.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
												.addComponent(lblNewLabel)
												.addGap(18)
												.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))))))
							.addContainerGap(32, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton_1)
							.addPreferredGap(ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
							.addComponent(btnNewButton)
							.addGap(130))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(39)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(label))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblNewLabel)))
					.addGap(61)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(manJrb)
								.addComponent(lblNewLabel_1)
								.addComponent(femaleJrb)
								.addComponent(lblNewLabel_2))
							.addGap(31)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_2)))
						.addComponent(priseTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(65)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1))
					.addPreferredGap(ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addGap(51))
		);
		getContentPane().setLayout(groupLayout);
		fillBookType();
	}
/**
 * 图书添加
 * @param evt
 */
	private void bookAddActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String bookName = this.bookNameTxt.getText();
		String bookDesc = this.bookDescTxt.getText();
		String author = this.authorTxt.getText();
		String prise = this.priseTxt.getText();
		if (StringUtil.isEmpty(bookName)){
			JOptionPane.showMessageDialog(null,"图书名称不能为空");
			return;
		}
		if (StringUtil.isEmpty(author)){
			JOptionPane.showMessageDialog(null,"图书作者不能为空");
			return;
		}
		if (StringUtil.isEmpty(prise)){
			JOptionPane.showMessageDialog(null,"图书价格不能为空");
			return;
		}
		String sex = "";
		if(manJrb.isSelected()){
			sex = "男";
		}else{
			sex = "女";
		}
//		获取用户输入的图书类型
		BookType bookType = (BookType)bookTypeJcb.getSelectedItem();
		int bookTypeId = bookType.getId();
		Book book = new Book(bookName,author,sex, Float.parseFloat(prise),bookDesc,bookTypeId);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int n =bookDao.add(con, book);
			if (n==1){
			JOptionPane.showMessageDialog(null,"图书添加成功");
//			添加成功,重置文本
			resetValue();
			}else{
				JOptionPane.showMessageDialog(null,"图书添加失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

/**
 * 初始化图书类别下拉框
 */
	private void fillBookType(){
		Connection con = null;
		BookType bookType = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = bookTypeDao.list(con, new BookType());
			while(rs.next()){
				bookType = new BookType();
				bookType.setId(rs.getInt("id"));
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				this.bookTypeJcb.addItem(bookType);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 重置表单
	 */
		private void resetValue(){
			this.bookNameTxt.setText("");
			this.bookDescTxt.setText("");
			this.authorTxt.setText("");
			this.priseTxt.setText("");
			this.manJrb.setSelected(true);
//			选择图书类型下拉框每次为一
			if(bookTypeJcb.getItemCount()>0){
				this.bookTypeJcb.setSelectedIndex(0);
			}
		}
}

三.效果展示
1.登录界面
在这里插入图片描述
2.图书添加
在这里插入图片描述
3.图书管理
在这里插入图片描述
4.图书类别管理
在这里插入图片描述
源码QQ群:938064268

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-10-02 14:56:25  更:2021-10-02 14:56:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 22:57:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码