图书信息管理java实现代码

网友投稿 270 2023-02-26


图书信息管理java实现代码

图书信息管理与读者信息管理相似,共包括两部分,图书添加和图书查询与修改,图书信息添加要求正确输入图书的八项信息,并且ISBN编号唯一,实现过程中要将ISBN作为主键。下面看实现过程:

数据库中书籍:

添加书籍过程:

数据库内容增加:

本模块用到标签面板,可以很方便的查询所需要的信息。修改过程输入ISBN编号后按回车键将在下方显示所有的图书信息,然后修改图书信息后所有内容就会更新。

图书查询:

图书信息修改:

添加图书源代码如下:AddBook.java

package pdsu.bbm.view;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Iterator;

import java.util.List;

import javax.swing.ButtonGroup;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import pdsu.bbm.dao.BookDao;

import pdsu.bbm.dao.BookTypeDao;

import pdsu.bbm.model.Book;

import pdsu.bbm.model.BookType;

public class AddBook extends JFrame implements ActionListener {

private JPanel contentPane;

private JLabel label_1;

private JLabel lbll;

private JLabel label_2;

private JLabel label_3;

private JLabel label_4;

private JLabel label_5;

JPanel centerpPanel;

JButton button1, button2, button3;

private JComboBox comboBox_1;

Image icon = new ImageIcon("image/icon.png").getImage();

private JTextField ISBNfiled;

private JTextField publishField;

private JTextField publishDateField;

private JTextField publishTime;

private JTextField unitPriceField;

private JTextField bookNameField;

private JTextField authorFiled;

public static void main(String[] args) {

new AddBook();

}

public AddBook() {

setTitle("添加图书信息");

setSize(555, 333);

setIconImage(icon);

setLocationRelativeTo(null);

setTitle("添加图书信息");

setSize(555, 334);

setLocationRelativeTo(null);

setVisible(true);

centerpPanel = new JPanel();

centerpPanel.setLayout(null);

JLabel label1 = new JLabel("ISBN:");

label1.setFont(new Font("宋体", Font.PLAIN, 20));

label1.setBounds(71, 26, 95, 42);

centerpPanel.add(label1);

ISBNfiled = new JTextField();

ISBNfiled.setBounds(120, 30, 110, 30);

centerpPanel.add(ISBNfiled);

ISBNfiled.setColumns(10);

JLabel label = new JLabel("类 别:");

label.setFont(new Font("宋体", Font.PLAIN, 20));

label.setBounds(306, 30, 116, 35);

centerpPanel.add(label);

label_1 = new JLabel("书 名:");

label_1.setFont(new Font("宋体", Font.PLAIN, 20));

label_1.setBounds(50, 75, 154, 50);

centerpPanel.add(label_1);

lbll = new JLabel("作 者:");

lbll.setFont(new Font("宋体", Font.PLAIN, 20));

lbll.setBounds(306, 75, 137, 50);

centerpPanel.add(lbll);

label_2 = new JLabel("出版社:");

label_2.setFont(new Font("宋体", Font.PLAIN, 20));

label_2.setBounds(50, 130, 154, 50);

centerpPanel.add(label_2);

label_3 = new JLabel("出版日期:");

label_3.setFont(new Font("宋体", Font.PLAIN, 20));

label_3.setBounds(285, 135, 137, 50);

centerpPanel.add(label_3);

publishField = new JTextField();

publishField.setColumns(10);

publishField.setBounds(120, 143, 110, 30);

centerpPanel.add(publishField);

publishDateField = new JTextField();

publishDateField.setColumns(10);

publishDateField.setBounds(380, 143, 120, 30);

centerpPanel.add(publishDateField);

label_4 = new JLabel("印刷次数:");

label_4.setFont(new Font("宋体", Font.PLAIN, 20));

label_4.setBounds(28, 190, 154, 50);

centerpPanel.add(label_4);

publishTime = new JTextField();

publishTime.setColumns(10);

publishTime.setBounds(120, 203, 110, 30);

centerpPanel.add(publishTime);

label_5 = new JLabel("单 价:");

label_5.setFont(new Font("宋体", Font.PLAIN, 20));

label_5.setBounds(305, 194, 84, 42);

centerpPanel.add(label_5);

unitPriceField = new JTextField();

unitPriceField.setColumns(10);

unitPriceField.setBounds(380, 203, 120, 30);

centerpPanel.add(unitPriceField);

getContentPane().add(centerpPanel, BorderLayout.CENTER);

bookNameField = new JTextField();

bookNameField.setColumns(10);

bookNameField.setBounds(120, 86, 110, 30);

centerpPanel.add(bookNameField);

authorFiled = new JTextField();

authorFiled.setColumns(10);

FSQCWjqP authorFiled.setBounds(380, 86, 120, 30);

centerpPanel.add(authorFiled);

List rs = BookTypeDao.selectBookType();

Iterator iterator = rs.iterator();

String[] AllTypeName = new String[rs.size()];

int i = 0;

while (iterator.hasNext()) {

String typename = iterator.next().getTypename();

AllTypeName[i] = typename;

i++;

}

comboBox_1 = new JComboBox(AllTypeName);

comboBox_1.setBounds(380, 30, 120, 30);

comboBox_1.setFont(new Font("宋体", Font.PLAIN, 16));

centerpPanel.add(comboBox_1);

ButtonGroup group = new ButtonGroup();

JPanel panel2 = new JPanel();

panel2.setLayout(new FlowLayout());

button1 = new JButton("添加");

button1.setFont(new Font("宋体", Font.PLAIN, 20));

button2 = new JButton("关闭");

button2.setFont(new Font("宋体", Font.PLAIN, 20));

button3 = new JButton("重置");

button3.setFont(new Font("宋体", Font.PLAIN, 20));

panel2.add(button1);

panel2.add(button3);

panel2.add(button2);

button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

getContentPane().add(panel2, BorderLayout.SOUTH);

setVisible(true);

}

@SuppressWarnings("unused")

public void actionPerformed(ActionEvent e) {

if (e.getSource() == button1) {

String Isbn = ISBNfiled.getText().toString();

String bookname = bookNameField.getText();

String author = authorFiled.getText();

String selectType = comboBox_1.getSelectedItem().toString();

String publish = publishField.getText();

String publishdate = publishDateField.getText();

String time = publishTime.getText().trim();

String price = unitPriceField.getText().trim();

if (Isbn.equals("") || bookname.equals("") || author.equals("")

|| selectType.equals("") || publish.equals("")

|| publishdate.equals("") || time.equals("")

|| price.equals("")) {

JOptionPane.showMessageDialog(this, "请输入完整信息");

return;

} else {

BookType bt = new BookType();

Book book = new Book();

book.setISBN(Isbn);

book.setBookname(bookname);

book.setAuthor(author);

bt.setTypename(selectType);

int m = 0;

List list = BookTypeDao

.selectIdByTypename(selectType);

Iterator ite = list.iterator();

while (ite.hasNext()) {

m = ite.next().getId();

}

book.setTypeid(m + "");

book.setPublish(publish);

try {

book.setPublishtime(Integer.parseInt(time));

book.setUnitprice(Integer.parseInt(price));

} catch (NumberFormatException e1) {

e1.printStackTrace();

}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date regDate = null;

try {

regDate =sdf.parse(publishdate);

} catch (ParseException e1) {

JOptionPane.showMessageDialog(this, "请输入正确的日期");

publishDateField.setText("");

}

book.setPublishdate(regDate);

if(regDate==null) return;

if (BookDao.selectBookByISBN(Isbn) != null) {

JOptionPane.showMessageDialog(this, "该ISBN编号已存在");

return;

} else if (BookDao.insertBook(book) != 0) {

JOptionPane.showMessageDialog(this, "添加成功");

} else

JOptionPane.showMessageDialog(this, "添加失败");

}

}

if (e.getSource() == button3) {

ISBNfiled.setText("");

bookNameField.setText("");

authorFiled.setText("");

publishField.setText("");

publishDateField.setText("");

publishTime.setText("");

unitPriceField.setText("");

}

if (e.getSource() == button2) {

dispose();

new MainWindow();

}

}

}

图书查询与修改源代码:BookSelectandModify.java

package pdsu.bbm.view;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Iterator;

import java.util.List;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.jscrollPane;

import javax.swing.JTabbedPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.table.TableModel;

import pdsu.bbm.dao.BookDao;

import pdsu.bbm.dao.BookTypeDao;

import pdsu.bbm.model.Book;

import pdsu.bbm.model.BookType;

public class BookSelectandModify extends JFrame implements ActionListener {

// 设置序列号

private static final long serialVersionUID = 1L;

private JTabbedPane jtabbedPane; // 标签面板

// selectJP查询面板 select_conditionJP下拉列表面板 select_resultJP结果按钮面板updateJP修改面板

// updateJP修改面板,bookJP中间面板 buttonJP2按钮面板

private JPanel selectJP, select_conditionJP, select_resultJP, buttonJP1,

bookJP, updateJP, buttonJP2;//

private JTextField selectJTF, ISBNJTF, booknameJTF, authorJTF,

printtimeJTF, publishJTF, publishdateJTF, unitpriceJTF;

private JLabel ISBNJL, booknameJL, authorJL, categoryJL, printtimeJL,

publishJL, publishdateJL, unitpriceJL;

// 重点!

private JTable jtable;// 定义表格

private JComboBox choiceJCB, booktypeJCB;

private JScrollPane jscrollPane;

private JButton selectJB, exitJB, updateJB, resetJB;// 查询按钮,退出按钮,修改按钮,关闭按钮

private TableModel getSelect;

"出版日期", "印刷次数", "单价" };

Image icon = new ImageIcon("image/icon.png").getImage();

public BookSelectandModify() {

super();

setIconImage(icon);

setTitle("图书查询与修改");

setBounds(100, 100, 555, 400);

setLocationRelativeTo(null);

// JTabbedPane选项卡

jtabbedPane = new JTabbedPane();

add(jtabbedPane);

selectJP = new JPanel();

selectJP.setLayout(new BorderLayout());

jtabbedPane.add("图书信息查询", selectJP);

// 查询条件面板

select_conditionJP = new JPanel();

choiceJCB = new JComboBox();

for (int i = 0; i < array.length; i++) {

choiceJCB.addItem(array[i]);

}

select_conditionJP.add(choiceJCB);

selectJTF = new JTextField(20);

select_conditionJP.add(selectJTF);

selectJP.add(select_conditionJP, BorderLayout.NORTH);

// 查询结果面板

select_resultJP = new JPanel();

BookDao dao = new BookDao();

List list = dao.selectBook();

jtable = new JTable(getSelect(list), title);

// !设置表格大小不变

jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// !设置列宽

jtable.getColumnModel().getColumn(4).setPreferredWidth(175);

jscrollPane = new JScrollPane(jtable);// 把表格加入滚动面板

// 显示滚动面板边框

jscrollPane.setPreferredSize(new Dimension(450, 170));

select_resultJP.add(jscrollPane);

selectJP.add(select_resultJP, BorderLayout.CENTER);

// 查询按钮面板

buttonJP1 = new JPanel();

selectJB = new JButton("查询");

selectJB.setFont(new Font("宋体", Font.PLAIN, 20));

selectJB.addActionListener(this);

buttonJP1.add(selectJB);

exitJB = new JButton("退出");

exitJB.setFont(new Font("宋体", Font.PLAIN, 20));

exitJB.addActionListener(this);

buttonJP1.add(exitJB);

selectJP.add(buttonJP1, BorderLayout.SOUTH);

// 信息修改页面

updateJP = new JPanel();

updateJP.setLayout(new BorderLayout());

jtabbedPane.addTab("图书信息修改", updateJP);

bookJP = new JPanel();

final GridLayout gridLayout = new GridLayout(8, 2);

// 设置边框间的距离

gridLayout.setVgap(8);

gridLayout.setHgap(8);

bookJP.setLayout(gridLayout);

ISBNJL = new JLabel("ISBN:");

ISBNJL.setFont(new Font("宋体",Font.PLAIN,20));

ISBNJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(ISBNJL);

ISBNJTF = new JTextField(20);

ISBNJTF.addActionListener(this);

bookJP.add(ISBNJTF);

categoryJL = new JLabel("类 别:");

categoryJL.setFont(new Font("宋体",Font.PLAIN,20));

categoryJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(categoryJL);

// 下拉列表

List list1 = BookTypeDao.selectBookType();

Iterator it = list1.iterator();

String ty[] = new String[list1.size()];

int i = 0;

booktypeJCB = new JComboBox();

while (it.hasNext()) {

ty[i] = it.next().getTypename();

booktypeJCB.addItem(ty[i]);

i++;

}

bookJP.add(booktypeJCB);

booknameJL = new JLabel("书 名:");

booknameJL.setFont(new Font("宋体",Font.PLAIN,20));

booknameJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(booknameJL);

booknameJTF = new JTextField();

booknameJTF.setColumns(20);

bookJP.add(booknameJTF);

authorJL = new JLabel("作 者:");

authorJL.setFont(new Font("宋体",Font.PLAIN,20));

authorJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(authorJL);

authorJTF = new JTextField();

authorJTF.setColumns(20);

bookJP.add(authorJTF);

publishJL = new JLabel("出版社:");

publishJL.setFont(new Font("宋体",Font.PLAIN,20));

publishJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(publishJL);

publishJTF = new JTextField();

bookJP.add(publishJTF);

publishdateJL = new JLabel("出版日期:");

publishdateJL.setFont(new Font("宋体",Font.PLAIN,20));

publishdateJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(publishdateJL);

publishdateJTF = new JTextField();

publishdateJTF.setHorizontalAlignment(SwingConstants.LEFT);

bookJP.add(publishdateJTF);

printtimeJL = new JLabel("印刷次数:");

printtimeJL.setFont(new Font("宋体",Font.PLAIN,20));

printtimeJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(printtimeJL);

printtimeJTF = new JTextField();

bookJP.add(printtimeJTF);

unitpriceJL = new JLabel("单 价:");

unitpriceJL.setFont(new Font("宋体",Font.PLAIN,20));

unitpriceJL.setHorizontalAlignment(SwingConstants.CENTER);

bookJP.add(unitpriceJL);

unitpriceJTF = new JTextField();

bookJP.add(unitpriceJTF);

// 按钮面板

// 按钮面板设计

buttonJP2 = new JPanel();

updateJB = new JButton("修改");

updateJB.setFont(new Font("宋体", Font.PLAIN, 20));

updateJB.addActionListener(this);

resetJB = new JButton("关闭");

resetJB.setFont(new Font("宋体", Font.PLAIN, 20));

resetJB.addActionListener(this);

buttonJP2.add(updateJB);

buttonJP2.add(resetJB);

updateJP.add(bookJP, BorderLayout.CENTEFSQCWjqPR);

updateJP.add(buttonJP2, BorderLayout.SOUTH);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

// 将查询表格加入面板

private Object[][] getSelect(List list) {

// TODO Auto-generated method stub

Object[][] objects = new Object[list.size()][title.length];

for (int i = 0; i < list.size(); i++) {

Book book = list.get(i);

objects[i][0] = book.getISBN();

objects[i][1] = book.getTypeid();// 图书序列

objects[i][2] = book.getBookname();// 图书名称

objects[i][4] = book.getPublish();// 出版社

objects[i][5] = book.getPublishdate();// 出版日期

objects[i][6] = book.getPublishtime();// 印刷次数

objects[i][7] = book.getUnitprice();// 单价

}

return objects;

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==ISBNJTF){

String isbn=ISBNJTF.getText();

String typename=null;

String id=BookDao.selectBookByISBN(isbn).getTypeid();

typename=BookTypeDao.selectById(id);

System.out.println(typename);

booktypeJCB.setSelectedItem(typename);

booknameJTF.setText(BookDao.selectBookByISBN(isbn).getBookname());

authorJTF.setText(BookDao.selectBookByISBN(isbn).getAuthor());

publishJTF.setText(BookDao.selectBookByISBN(isbn).getPublish());

publishdateJTF.setText(BookDao.selectBookByISBN(isbn).getPublishdate()+"");

printtimeJTF.setText(BookDao.selectBookByISBN(isbn).getPublishtime()+"");

unitpriceJTF.setText(BookDao.selectBookByISBN(isbn).getUnitprice()+"");

}

if (e.getSource() == selectJB) {// 按照ISBN编码查找

int r = choiceJCB.getSelectedIndex();

if (r == 0) {

String name = selectJTF.getText().trim();

// 强制转换为线性表类型

List list = new ArrayList();

Book book = BookDao.selectBookByISBN(name);

if (book == null) {

JOptionPane.showMessageDialog(this, "该编码不存在!");

} else {

list.add(book);

Object[][] data = getSelect(list);

jtable = new JTable(data, title);

jtable.getColumnModel().getColumn(4).setPreferredWidth(175);

jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

jscrollPane.setViewportView(jtable);

}

}

if (r == 1) {// 根据图书名称查询信息

String name = selectJTF.getText().toString();

List list = BookDao.selectBookByName(name);

// 判断线性表是否为空

if (list == null || list.size() == 0) {

JOptionPane.showMessageDialog(this, "该图书不存在!");

} else {

Object[][] data = getSelect(list);

jtable = new JTable(data, title);

tianjia();

}

}

if (r == 2) {// 根据图书序列查询信息

String name = selectJTF.getText().toString();

List list = BookDao.selectBookByType(name);

// 判断线性表是否为空

if (list == null || list.size() == 0) {

JOptionPane.showMessageDialog(this, "该图书不存在!");

} else {

Object[][] data = getSelect(list);

jtable = new JTable(data, title);

tianjia();

}

}

String name = selectJTF.getText().toString();

List list = BookDao.selectBookByAuthor(name);

// 判断线性表是否为空

if (list == null || list.size() == 0) {

JOptionPane.showMessageDialog(this, "该图书不存在!");

} else {

Object[][] data = getSelect(list);

jtable = new JTable(data, title);

tianjia();

}

}

if (r == 4) {// 根据出版社进行查找

String name = selectJTF.getText().toString();

List list = BookDao.selectBookByPublish(name);

if (list == null || list.size() == 0) {

JOptionPane.showMessageDialog(this, "该图书不存在!");

} else {

Object[][] data = getSelect(list);

jtable = new JTable(data, title);

tianjia();

}

}

if(r==5){

List list =BookDao.selectBook();

}

}

if (e.getSource() == updateJB) {

String ISBN = ISBNJTF.getText().trim();

String typename = ((String) booktypeJCB.getSelectedItem()).trim();

String id = BookTypeDao.selectByTypename(typename);

String bookname = booknameJTF.getText();

String author = authorJTF.getText();

String publish = publishJTF.getText();

String publishdate = publishdateJTF.getText();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date regDate = null;

try {

regDate = sdf.parse(publishdate);

} catch (Exception e2) {

e2.printStackTrace();

JOptionPane.showMessageDialog(this, "请输入正确的日期格式!");

}

if (regDate == null)

return;

int publishtime = Integer.parseInt(printtimeJTF.getText());

int unitprice = Integer.parseInt(unitpriceJTF.getText());

// 封装对象

Book book = new Book();

book.setISBN(ISBN);

book.setTypeid(id);

book.setBookname(bookname);

book.setAuthor(author);

book.setPublish(publish);

book.setPublishdate(regDate);

book.setPublishtime(publishtime);

book.setUnitprice(unitprice);

book.setTypename(typename);

int result = BookDao.update(book);

List list2 = new ArrayList();

list2.add(book);

// System.out.println(list2);

if (result == 1) {

JOptionPane.showMessageDialog(this, "修改成功!");

BookDao bk=new BookDao();

List list =bk.selectBook();

jtable =new JTable(getSelect(list),title);

tianjia();

} else {

JOptionPane.showMessageDialog(this, "修改失败!");

}

}

if (e.getSource() == resetJB) {

dispose();

new MainWindow();

}

if (e.getSource() == exitJB) {

dispose();

new MainWindow();

}

}

private void tianjia() {

jtable.getColumnModel().getColumn(4).setPreferredWidth(175);

jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

jscrollPane.setViewportView(jtable);

}

public static void main(String[] args) {

new BookSelectandModify();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:web前端vue实现插值文本和输出原始html
下一篇:spring cloud实现前端跨域问题的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~