惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!
1、创建数据表t_book(略)
3、创建BookAddInterFrm(略)
2、创建BookModel
package com.java1234.model; public class Book { private int id; private String bookName; private String author; private String sex; private float price; private String bookDesc; private int bookTypeId; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the bookName */ public String getBookName() { return bookName; } /** * @param bookName the bookName to set */ public void setBookName(String bookName) { this.bookName = bookName; } /** * @return the author */ public String getAuthor() { return author; } /** * @param author the author to set */ public void setAuthor(String author) { this.author = author; } /** * @return the sex */ public String getSex() { return sex; } /** * @param sex the sex to set */ public void setSex(String sex) { this.sex = sex; } /** * @return the price */ public float getPrice() { return price; } /** * @param price the price to set */ public void setPrice(float price) { this.price = price; } /** * @return the bookDesc */ public String getBookDesc() { return bookDesc; } /** * @param bookDesc the bookDesc to set */ public void setBookDesc(String bookDesc) { this.bookDesc = bookDesc; } /** * @return the bookTypeId */ public int getBookTypeId() { return bookTypeId; } /** * @param bookTypeId the bookTypeId to set */ public void setBookTypeId(int bookTypeId) { this.bookTypeId = bookTypeId; } }
4、在MainFrm中添加图书添加链接
private void jmi_BookAddActionPerformed(java.awt.event.ActionEvent evt) { //添加图书窗口 BookAddInterFrm BookAddInterFrm = new BookAddInterFrm(); BookAddInterFrm.setVisible(true); table.add(BookAddInterFrm); }
5、添加jcb_BookType下拉列表框
/** * 添加jcb_BookType下拉列表框 */ private void fillBookType() { BookType bookType=null; Connection con=null; try { con=dbUtil.getCon(); ResultSet rs=bookTypeDao.bookTypeList(con, new BookType()); while(rs.next()){ bookType=new BookType(); bookType.setId(rs.getInt("id")); bookType.setBookTypeName(rs.getString("bookTypeName")); jcb_bookType.addItem(bookType); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
修改toString方法,设定返回值
public String toString() { return getBookTypeName(); }
6、设置默认属性
//设置窗口的位置 setLocation(500, 150); //设置默认值 jrb_man.setSelected(true); if(jcb_bookType.getItemCount()>0){ jcb_bookType.setSelectedIndex(0); } //显示下拉列表框 fillBookType();
7、编写重置代码
private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) { resetValue(); } //重置 private void resetValue(){ bookNameTxt.setText(""); authorTxt.setText(""); jrb_man.setSelected(true); if (jcb_bookType.getItemCount() > 0) { jcb_bookType.setSelectedIndex(0); } priceTxt.setText(""); bookDescTxt.setText(""); }
8、编写添加代码
/** * 图书添加 * @param con * @param book * @return int * @throws Exception */ public int bookAdd(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(); }
/** * 图书添加事件 */ private void jb_addActionPerformed(java.awt.event.ActionEvent evt) { //获取数据 String bookName=bookNameTxt.getText(); String author=authorTxt.getText(); String price=priceTxt.getText(); String bookDesc=bookDescTxt.getText(); String sex=""; if(jrb_man.isSelected()){ sex="男"; } if(jrb_female.isSelected()){ sex="女"; } BookType bookType=(BookType) jcb_bookType.getSelectedItem(); int bookTypeId=bookType.getId(); //判断是否为空 if(StringUtil.isEmpty(bookName)){ JOptionPane.showMessageDialog(null, "图书名称不能为空"); return; } if(StringUtil.isEmpty(author)){ JOptionPane.showMessageDialog(null, "图书作者不能为空"); return; } if(StringUtil.isEmpty(price)){ JOptionPane.showMessageDialog(null, "图书价格不能为空"); return; } //封装 Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookDesc,bookTypeId); //连接数据库,并添加数据 Connection con=null; try { con=dbUtil.getCon(); int addNum=bookDao.bookAdd(con, book); if(addNum==1){ JOptionPane.showMessageDialog(null, "添加成功"); resetValue(); }else{ JOptionPane.showMessageDialog(null, "添加失败"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "添加失败"); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }