查看: 3210|回复: 0

[Java代码] 图书信息管理java实现代码

发表于 2018-3-4 10:27:12

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

数据库中书籍:

添加书籍过程:

数据库内容增加:

图书的查询与修改:查询过程可以根据不同的条件查询,如ISBN,作者,出版社,图书名称等,默认将所有图书全部显示在列表中,输入查询条件后再表格中显示查询结果。

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

图书查询:

图书信息修改:

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

  1. package pdsu.bbm.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.FlowLayout;
  4. import java.awt.Font;
  5. import java.awt.Image;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import javax.swing.ButtonGroup;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextField;
  22. import pdsu.bbm.dao.BookDao;
  23. import pdsu.bbm.dao.BookTypeDao;
  24. import pdsu.bbm.model.Book;
  25. import pdsu.bbm.model.BookType;
  26. public class AddBook extends JFrame implements ActionListener {
  27. private JPanel contentPane;
  28. private JLabel label_1;
  29. private JLabel lbll;
  30. private JLabel label_2;
  31. private JLabel label_3;
  32. private JLabel label_4;
  33. private JLabel label_5;
  34. JPanel centerpPanel;
  35. JButton button1, button2, button3;
  36. private JComboBox comboBox_1;
  37. Image icon = new ImageIcon("image/icon.png").getImage();
  38. private JTextField ISBNfiled;
  39. private JTextField publishField;
  40. private JTextField publishDateField;
  41. private JTextField publishTime;
  42. private JTextField unitPriceField;
  43. private JTextField bookNameField;
  44. private JTextField authorFiled;
  45. public static void main(String[] args) {
  46. new AddBook();
  47. }
  48. public AddBook() {
  49. setTitle("添加图书信息");
  50. setSize(555, 333);
  51. setIconImage(icon);
  52. setLocationRelativeTo(null);
  53. setTitle("添加图书信息");
  54. setSize(555, 334);
  55. setLocationRelativeTo(null);
  56. setVisible(true);
  57. centerpPanel = new JPanel();
  58. centerpPanel.setLayout(null);
  59. JLabel label1 = new JLabel("ISBN:");
  60. label1.setFont(new Font("宋体", Font.PLAIN, 20));
  61. label1.setBounds(71, 26, 95, 42);
  62. centerpPanel.add(label1);
  63. ISBNfiled = new JTextField();
  64. ISBNfiled.setBounds(120, 30, 110, 30);
  65. centerpPanel.add(ISBNfiled);
  66. ISBNfiled.setColumns(10);
  67. JLabel label = new JLabel("类 别:");
  68. label.setFont(new Font("宋体", Font.PLAIN, 20));
  69. label.setBounds(306, 30, 116, 35);
  70. centerpPanel.add(label);
  71. label_1 = new JLabel("书 名:");
  72. label_1.setFont(new Font("宋体", Font.PLAIN, 20));
  73. label_1.setBounds(50, 75, 154, 50);
  74. centerpPanel.add(label_1);
  75. lbll = new JLabel("作 者:");
  76. lbll.setFont(new Font("宋体", Font.PLAIN, 20));
  77. lbll.setBounds(306, 75, 137, 50);
  78. centerpPanel.add(lbll);
  79. label_2 = new JLabel("出版社:");
  80. label_2.setFont(new Font("宋体", Font.PLAIN, 20));
  81. label_2.setBounds(50, 130, 154, 50);
  82. centerpPanel.add(label_2);
  83. label_3 = new JLabel("出版日期:");
  84. label_3.setFont(new Font("宋体", Font.PLAIN, 20));
  85. label_3.setBounds(285, 135, 137, 50);
  86. centerpPanel.add(label_3);
  87. publishField = new JTextField();
  88. publishField.setColumns(10);
  89. publishField.setBounds(120, 143, 110, 30);
  90. centerpPanel.add(publishField);
  91. publishDateField = new JTextField();
  92. publishDateField.setColumns(10);
  93. publishDateField.setBounds(380, 143, 120, 30);
  94. centerpPanel.add(publishDateField);
  95. label_4 = new JLabel("印刷次数:");
  96. label_4.setFont(new Font("宋体", Font.PLAIN, 20));
  97. label_4.setBounds(28, 190, 154, 50);
  98. centerpPanel.add(label_4);
  99. publishTime = new JTextField();
  100. publishTime.setColumns(10);
  101. publishTime.setBounds(120, 203, 110, 30);
  102. centerpPanel.add(publishTime);
  103. label_5 = new JLabel("单 价:");
  104. label_5.setFont(new Font("宋体", Font.PLAIN, 20));
  105. label_5.setBounds(305, 194, 84, 42);
  106. centerpPanel.add(label_5);
  107. unitPriceField = new JTextField();
  108. unitPriceField.setColumns(10);
  109. unitPriceField.setBounds(380, 203, 120, 30);
  110. centerpPanel.add(unitPriceField);
  111. getContentPane().add(centerpPanel, BorderLayout.CENTER);
  112. bookNameField = new JTextField();
  113. bookNameField.setColumns(10);
  114. bookNameField.setBounds(120, 86, 110, 30);
  115. centerpPanel.add(bookNameField);
  116. authorFiled = new JTextField();
  117. authorFiled.setColumns(10);
  118. authorFiled.setBounds(380, 86, 120, 30);
  119. centerpPanel.add(authorFiled);
  120. List<BookType> rs = BookTypeDao.selectBookType();
  121. Iterator<BookType> iterator = rs.iterator();
  122. String[] AllTypeName = new String[rs.size()];
  123. int i = 0;
  124. while (iterator.hasNext()) {
  125. String typename = iterator.next().getTypename();
  126. AllTypeName[i] = typename;
  127. i++;
  128. }
  129. comboBox_1 = new JComboBox(AllTypeName);
  130. comboBox_1.setBounds(380, 30, 120, 30);
  131. comboBox_1.setFont(new Font("宋体", Font.PLAIN, 16));
  132. centerpPanel.add(comboBox_1);
  133. ButtonGroup group = new ButtonGroup();
  134. JPanel panel2 = new JPanel();
  135. panel2.setLayout(new FlowLayout());
  136. button1 = new JButton("添加");
  137. button1.setFont(new Font("宋体", Font.PLAIN, 20));
  138. button2 = new JButton("关闭");
  139. button2.setFont(new Font("宋体", Font.PLAIN, 20));
  140. button3 = new JButton("重置");
  141. button3.setFont(new Font("宋体", Font.PLAIN, 20));
  142. panel2.add(button1);
  143. panel2.add(button3);
  144. panel2.add(button2);
  145. button1.addActionListener(this);
  146. button2.addActionListener(this);
  147. button3.addActionListener(this);
  148. getContentPane().add(panel2, BorderLayout.SOUTH);
  149. setVisible(true);
  150. }
  151. @SuppressWarnings("unused")
  152. public void actionPerformed(ActionEvent e) {
  153. if (e.getSource() == button1) {
  154. String Isbn = ISBNfiled.getText().toString();
  155. String bookname = bookNameField.getText();
  156. String author = authorFiled.getText();
  157. String selectType = comboBox_1.getSelectedItem().toString();
  158. String publish = publishField.getText();
  159. String publishdate = publishDateField.getText();
  160. String time = publishTime.getText().trim();
  161. String price = unitPriceField.getText().trim();
  162. if (Isbn.equals("") || bookname.equals("") || author.equals("")
  163. || selectType.equals("") || publish.equals("")
  164. || publishdate.equals("") || time.equals("")
  165. || price.equals("")) {
  166. JOptionPane.showMessageDialog(this, "请输入完整信息");
  167. return;
  168. } else {
  169. BookType bt = new BookType();
  170. Book book = new Book();
  171. book.setISBN(Isbn);
  172. book.setBookname(bookname);
  173. book.setAuthor(author);
  174. bt.setTypename(selectType);
  175. int m = 0;
  176. List<BookType> list = BookTypeDao
  177. .selectIdByTypename(selectType);
  178. Iterator<BookType> ite = list.iterator();
  179. while (ite.hasNext()) {
  180. m = ite.next().getId();
  181. }
  182. book.setTypeid(m + "");
  183. book.setPublish(publish);
  184. try {
  185. book.setPublishtime(Integer.parseInt(time));
  186. book.setUnitprice(Integer.parseInt(price));
  187. } catch (NumberFormatException e1) {
  188. e1.printStackTrace();
  189. }
  190. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  191. Date regDate = null;
  192. try {
  193. regDate =sdf.parse(publishdate);
  194. } catch (ParseException e1) {
  195. JOptionPane.showMessageDialog(this, "请输入正确的日期");
  196. publishDateField.setText("");
  197. }
  198. book.setPublishdate(regDate);
  199. if(regDate==null) return;
  200. if (BookDao.selectBookByISBN(Isbn) != null) {
  201. JOptionPane.showMessageDialog(this, "该ISBN编号已存在");
  202. return;
  203. } else if (BookDao.insertBook(book) != 0) {
  204. JOptionPane.showMessageDialog(this, "添加成功");
  205. } else
  206. JOptionPane.showMessageDialog(this, "添加失败");
  207. }
  208. }
  209. if (e.getSource() == button3) {
  210. ISBNfiled.setText("");
  211. bookNameField.setText("");
  212. authorFiled.setText("");
  213. publishField.setText("");
  214. publishDateField.setText("");
  215. publishTime.setText("");
  216. unitPriceField.setText("");
  217. }
  218. if (e.getSource() == button2) {
  219. dispose();
  220. new MainWindow();
  221. }
  222. }
  223. }
复制代码

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

  1. package pdsu.bbm.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.GridLayout;
  6. import java.awt.Image;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTabbedPane;
  23. import javax.swing.JTable;
  24. import javax.swing.JTextField;
  25. import javax.swing.SwingConstants;
  26. import javax.swing.table.TableModel;
  27. import pdsu.bbm.dao.BookDao;
  28. import pdsu.bbm.dao.BookTypeDao;
  29. import pdsu.bbm.model.Book;
  30. import pdsu.bbm.model.BookType;
  31. public class BookSelectandModify extends JFrame implements ActionListener {
  32. // 设置序列号
  33. private static final long serialVersionUID = 1L;
  34. private JTabbedPane jtabbedPane; // 标签面板
  35. // selectJP查询面板 select_conditionJP下拉列表面板 select_resultJP结果按钮面板updateJP修改面板
  36. // updateJP修改面板,bookJP中间面板 buttonJP2按钮面板
  37. private JPanel selectJP, select_conditionJP, select_resultJP, buttonJP1,
  38. bookJP, updateJP, buttonJP2;//
  39. private JTextField selectJTF, ISBNJTF, booknameJTF, authorJTF,
  40. printtimeJTF, publishJTF, publishdateJTF, unitpriceJTF;
  41. private JLabel ISBNJL, booknameJL, authorJL, categoryJL, printtimeJL,
  42. publishJL, publishdateJL, unitpriceJL;
  43. // 重点!
  44. private JTable jtable;// 定义表格
  45. private JComboBox choiceJCB, booktypeJCB;
  46. private JScrollPane jscrollPane;
  47. private JButton selectJB, exitJB, updateJB, resetJB;// 查询按钮,退出按钮,修改按钮,关闭按钮
  48. private TableModel getSelect;
  49. private String[] title = { "ISBN", "图书序列", "图书名称", "作者", " 出版社 ",
  50. "出版日期", "印刷次数", "单价" };
  51. Image icon = new ImageIcon("image/icon.png").getImage();
  52. public BookSelectandModify() {
  53. super();
  54. setIconImage(icon);
  55. setTitle("图书查询与修改");
  56. setBounds(100, 100, 555, 400);
  57. setLocationRelativeTo(null);
  58. // JTabbedPane选项卡
  59. jtabbedPane = new JTabbedPane();
  60. add(jtabbedPane);
  61. selectJP = new JPanel();
  62. selectJP.setLayout(new BorderLayout());
  63. jtabbedPane.add("图书信息查询", selectJP);
  64. // 查询条件面板
  65. select_conditionJP = new JPanel();
  66. choiceJCB = new JComboBox();
  67. String array[] = { "ISBN", "图书名称", "图书序列", "作者", "出版社","所有" };// 设置为一维数组
  68. for (int i = 0; i < array.length; i++) {
  69. choiceJCB.addItem(array[i]);
  70. }
  71. select_conditionJP.add(choiceJCB);
  72. selectJTF = new JTextField(20);
  73. select_conditionJP.add(selectJTF);
  74. selectJP.add(select_conditionJP, BorderLayout.NORTH);
  75. // 查询结果面板
  76. select_resultJP = new JPanel();
  77. BookDao dao = new BookDao();
  78. List<Book> list = dao.selectBook();
  79. jtable = new JTable(getSelect(list), title);
  80. // !设置表格大小不变
  81. jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  82. // !设置列宽
  83. jtable.getColumnModel().getColumn(4).setPreferredWidth(175);
  84. jscrollPane = new JScrollPane(jtable);// 把表格加入滚动面板
  85. // 显示滚动面板边框
  86. jscrollPane.setPreferredSize(new Dimension(450, 170));
  87. select_resultJP.add(jscrollPane);
  88. selectJP.add(select_resultJP, BorderLayout.CENTER);
  89. // 查询按钮面板
  90. buttonJP1 = new JPanel();
  91. selectJB = new JButton("查询");
  92. selectJB.setFont(new Font("宋体", Font.PLAIN, 20));
  93. selectJB.addActionListener(this);
  94. buttonJP1.add(selectJB);
  95. exitJB = new JButton("退出");
  96. exitJB.setFont(new Font("宋体", Font.PLAIN, 20));
  97. exitJB.addActionListener(this);
  98. buttonJP1.add(exitJB);
  99. selectJP.add(buttonJP1, BorderLayout.SOUTH);
  100. // 信息修改页面
  101. updateJP = new JPanel();
  102. updateJP.setLayout(new BorderLayout());
  103. jtabbedPane.addTab("图书信息修改", updateJP);
  104. bookJP = new JPanel();
  105. final GridLayout gridLayout = new GridLayout(8, 2);
  106. // 设置边框间的距离
  107. gridLayout.setVgap(8);
  108. gridLayout.setHgap(8);
  109. bookJP.setLayout(gridLayout);
  110. ISBNJL = new JLabel("ISBN:");
  111. ISBNJL.setFont(new Font("宋体",Font.PLAIN,20));
  112. ISBNJL.setHorizontalAlignment(SwingConstants.CENTER);
  113. bookJP.add(ISBNJL);
  114. ISBNJTF = new JTextField(20);
  115. ISBNJTF.addActionListener(this);
  116. bookJP.add(ISBNJTF);
  117. categoryJL = new JLabel("类 别:");
  118. categoryJL.setFont(new Font("宋体",Font.PLAIN,20));
  119. categoryJL.setHorizontalAlignment(SwingConstants.CENTER);
  120. bookJP.add(categoryJL);
  121. // 下拉列表
  122. List<BookType> list1 = BookTypeDao.selectBookType();
  123. Iterator<BookType> it = list1.iterator();
  124. String ty[] = new String[list1.size()];
  125. int i = 0;
  126. booktypeJCB = new JComboBox();
  127. while (it.hasNext()) {
  128. ty[i] = it.next().getTypename();
  129. booktypeJCB.addItem(ty[i]);
  130. i++;
  131. }
  132. bookJP.add(booktypeJCB);
  133. booknameJL = new JLabel("书 名:");
  134. booknameJL.setFont(new Font("宋体",Font.PLAIN,20));
  135. booknameJL.setHorizontalAlignment(SwingConstants.CENTER);
  136. bookJP.add(booknameJL);
  137. booknameJTF = new JTextField();
  138. booknameJTF.setColumns(20);
  139. bookJP.add(booknameJTF);
  140. authorJL = new JLabel("作 者:");
  141. authorJL.setFont(new Font("宋体",Font.PLAIN,20));
  142. authorJL.setHorizontalAlignment(SwingConstants.CENTER);
  143. bookJP.add(authorJL);
  144. authorJTF = new JTextField();
  145. authorJTF.setColumns(20);
  146. bookJP.add(authorJTF);
  147. publishJL = new JLabel("出版社:");
  148. publishJL.setFont(new Font("宋体",Font.PLAIN,20));
  149. publishJL.setHorizontalAlignment(SwingConstants.CENTER);
  150. bookJP.add(publishJL);
  151. publishJTF = new JTextField();
  152. bookJP.add(publishJTF);
  153. publishdateJL = new JLabel("出版日期:");
  154. publishdateJL.setFont(new Font("宋体",Font.PLAIN,20));
  155. publishdateJL.setHorizontalAlignment(SwingConstants.CENTER);
  156. bookJP.add(publishdateJL);
  157. publishdateJTF = new JTextField();
  158. publishdateJTF.setHorizontalAlignment(SwingConstants.LEFT);
  159. bookJP.add(publishdateJTF);
  160. printtimeJL = new JLabel("印刷次数:");
  161. printtimeJL.setFont(new Font("宋体",Font.PLAIN,20));
  162. printtimeJL.setHorizontalAlignment(SwingConstants.CENTER);
  163. bookJP.add(printtimeJL);
  164. printtimeJTF = new JTextField();
  165. bookJP.add(printtimeJTF);
  166. unitpriceJL = new JLabel("单 价:");
  167. unitpriceJL.setFont(new Font("宋体",Font.PLAIN,20));
  168. unitpriceJL.setHorizontalAlignment(SwingConstants.CENTER);
  169. bookJP.add(unitpriceJL);
  170. unitpriceJTF = new JTextField();
  171. bookJP.add(unitpriceJTF);
  172. // 按钮面板
  173. // 按钮面板设计
  174. buttonJP2 = new JPanel();
  175. updateJB = new JButton("修改");
  176. updateJB.setFont(new Font("宋体", Font.PLAIN, 20));
  177. updateJB.addActionListener(this);
  178. resetJB = new JButton("关闭");
  179. resetJB.setFont(new Font("宋体", Font.PLAIN, 20));
  180. resetJB.addActionListener(this);
  181. buttonJP2.add(updateJB);
  182. buttonJP2.add(resetJB);
  183. updateJP.add(bookJP, BorderLayout.CENTER);
  184. updateJP.add(buttonJP2, BorderLayout.SOUTH);
  185. setVisible(true);
  186. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  187. }
  188. // 将查询表格加入面板
  189. private Object[][] getSelect(List<Book> list) {
  190. // TODO Auto-generated method stub
  191. Object[][] objects = new Object[list.size()][title.length];
  192. for (int i = 0; i < list.size(); i++) {
  193. Book book = list.get(i);
  194. objects[i][0] = book.getISBN();
  195. objects[i][1] = book.getTypeid();// 图书序列
  196. objects[i][2] = book.getBookname();// 图书名称
  197. objects[i][3] = book.getAuthor();// 图书作者
  198. objects[i][4] = book.getPublish();// 出版社
  199. objects[i][5] = book.getPublishdate();// 出版日期
  200. objects[i][6] = book.getPublishtime();// 印刷次数
  201. objects[i][7] = book.getUnitprice();// 单价
  202. }
  203. return objects;
  204. }
  205. public void actionPerformed(ActionEvent e) {
  206. // TODO Auto-generated method stub
  207. if(e.getSource()==ISBNJTF){
  208. String isbn=ISBNJTF.getText();
  209. String typename=null;
  210. String id=BookDao.selectBookByISBN(isbn).getTypeid();
  211. typename=BookTypeDao.selectById(id);
  212. System.out.println(typename);
  213. booktypeJCB.setSelectedItem(typename);
  214. booknameJTF.setText(BookDao.selectBookByISBN(isbn).getBookname());
  215. authorJTF.setText(BookDao.selectBookByISBN(isbn).getAuthor());
  216. publishJTF.setText(BookDao.selectBookByISBN(isbn).getPublish());
  217. publishdateJTF.setText(BookDao.selectBookByISBN(isbn).getPublishdate()+"");
  218. printtimeJTF.setText(BookDao.selectBookByISBN(isbn).getPublishtime()+"");
  219. unitpriceJTF.setText(BookDao.selectBookByISBN(isbn).getUnitprice()+"");
  220. }
  221. if (e.getSource() == selectJB) {// 按照ISBN编码查找
  222. int r = choiceJCB.getSelectedIndex();
  223. if (r == 0) {
  224. String name = selectJTF.getText().trim();
  225. // 强制转换为线性表类型
  226. List<Book> list = new ArrayList<Book>();
  227. Book book = BookDao.selectBookByISBN(name);
  228. if (book == null) {
  229. JOptionPane.showMessageDialog(this, "该编码不存在!");
  230. } else {
  231. list.add(book);
  232. Object[][] data = getSelect(list);
  233. jtable = new JTable(data, title);
  234. jtable.getColumnModel().getColumn(4).setPreferredWidth(175);
  235. jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  236. jscrollPane.setViewportView(jtable);
  237. }
  238. }
  239. if (r == 1) {// 根据图书名称查询信息
  240. String name = selectJTF.getText().toString();
  241. List<Book> list = BookDao.selectBookByName(name);
  242. // 判断线性表是否为空
  243. if (list == null || list.size() == 0) {
  244. JOptionPane.showMessageDialog(this, "该图书不存在!");
  245. } else {
  246. Object[][] data = getSelect(list);
  247. jtable = new JTable(data, title);
  248. tianjia();
  249. }
  250. }
  251. if (r == 2) {// 根据图书序列查询信息
  252. String name = selectJTF.getText().toString();
  253. List<Book> list = BookDao.selectBookByType(name);
  254. // 判断线性表是否为空
  255. if (list == null || list.size() == 0) {
  256. JOptionPane.showMessageDialog(this, "该图书不存在!");
  257. } else {
  258. Object[][] data = getSelect(list);
  259. jtable = new JTable(data, title);
  260. tianjia();
  261. }
  262. }
  263. if (r == 3) { // 根据作者进行查找
  264. String name = selectJTF.getText().toString();
  265. // 根据作者进行查找
  266. List<Book> list = BookDao.selectBookByAuthor(name);
  267. // 判断线性表是否为空
  268. if (list == null || list.size() == 0) {
  269. JOptionPane.showMessageDialog(this, "该图书不存在!");
  270. } else {
  271. Object[][] data = getSelect(list);
  272. jtable = new JTable(data, title);
  273. tianjia();
  274. }
  275. }
  276. if (r == 4) {// 根据出版社进行查找
  277. String name = selectJTF.getText().toString();
  278. List<Book> list = BookDao.selectBookByPublish(name);
  279. if (list == null || list.size() == 0) {
  280. JOptionPane.showMessageDialog(this, "该图书不存在!");
  281. } else {
  282. Object[][] data = getSelect(list);
  283. jtable = new JTable(data, title);
  284. tianjia();
  285. }
  286. }
  287. if(r==5){
  288. List<Book> list =BookDao.selectBook();
  289. }
  290. }
  291. if (e.getSource() == updateJB) {
  292. String ISBN = ISBNJTF.getText().trim();
  293. String typename = ((String) booktypeJCB.getSelectedItem()).trim();
  294. String id = BookTypeDao.selectByTypename(typename);
  295. String bookname = booknameJTF.getText();
  296. String author = authorJTF.getText();
  297. String publish = publishJTF.getText();
  298. String publishdate = publishdateJTF.getText();
  299. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  300. Date regDate = null;
  301. try {
  302. regDate = sdf.parse(publishdate);
  303. } catch (Exception e2) {
  304. e2.printStackTrace();
  305. JOptionPane.showMessageDialog(this, "请输入正确的日期格式!");
  306. }
  307. if (regDate == null)
  308. return;
  309. int publishtime = Integer.parseInt(printtimeJTF.getText());
  310. int unitprice = Integer.parseInt(unitpriceJTF.getText());
  311. // 封装对象
  312. Book book = new Book();
  313. book.setISBN(ISBN);
  314. book.setTypeid(id);
  315. book.setBookname(bookname);
  316. book.setAuthor(author);
  317. book.setPublish(publish);
  318. book.setPublishdate(regDate);
  319. book.setPublishtime(publishtime);
  320. book.setUnitprice(unitprice);
  321. book.setTypename(typename);
  322. int result = BookDao.update(book);
  323. List<Book> list2 = new ArrayList<Book>();
  324. list2.add(book);
  325. // System.out.println(list2);
  326. if (result == 1) {
  327. JOptionPane.showMessageDialog(this, "修改成功!");
  328. BookDao bk=new BookDao();
  329. List<Book> list =bk.selectBook();
  330. jtable =new JTable(getSelect(list),title);
  331. tianjia();
  332. } else {
  333. JOptionPane.showMessageDialog(this, "修改失败!");
  334. }
  335. }
  336. if (e.getSource() == resetJB) {
  337. dispose();
  338. new MainWindow();
  339. }
  340. if (e.getSource() == exitJB) {
  341. dispose();
  342. new MainWindow();
  343. }
  344. }
  345. private void tianjia() {
  346. jtable.getColumnModel().getColumn(4).setPreferredWidth(175);
  347. jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  348. jscrollPane.setViewportView(jtable);
  349. }
  350. public static void main(String[] args) {
  351. new BookSelectandModify();
  352. }
  353. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报