Swing入門級プロジェクト全過程実録第6講

19763 ワード

恒例の広告が出ると、初心者にとって本当に役に立ちます.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();

            }

        }

    }