zoukankan      html  css  js  c++  java
  • Swing入门级项目全程实录第6讲

     惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

    1、创建数据表t_book(略)
     
    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;
        }
        
    }
    3、创建BookAddInterFrm(略)
     
    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();
                }
            }
        }
  • 相关阅读:
    @Autowired mapper 层次 bean 带红线
    java 类加载机制 阿里面试题
    liunx 修改ssh 端口22
    通过mysqlbinlog 恢复数据
    网页命名规则
    子选择符 、相邻选择符 、 兄弟选择符 、 伪类选择符
    css的一些基础知识
    HTML5的表单所有type类型
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(复制)
    网站开发最常用的代码(复制)
  • 原文地址:https://www.cnblogs.com/cnmotive/p/3130471.html
Copyright © 2011-2022 走看看