mybatis.client.MyJFrame.java Source code

Java tutorial

Introduction

Here is the source code for mybatis.client.MyJFrame.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mybatis.client;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import mybatis.vo.MemVO;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 *
 * @author USER
 */
public class MyJFrame extends javax.swing.JFrame {

    private Reader r;
    private SqlSessionFactory factory;

    /**
     * Creates new form MyJFrame
     */
    public MyJFrame() {
        initComponents();
        init();

    }

    private void init() {
        try {
            // 1) mybatis  ?  

            r = Resources.getResourceAsReader("mybatis/config/config.xml");

            // 2) ?  ?? - factory ?
            factory = new SqlSessionFactoryBuilder().build(r);

            r.close();

            this.setTitle("~~");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("");
        jButton1.setPreferredSize(new java.awt.Dimension(81, 30));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("?");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("?");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(94, 94, 94)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49).addComponent(jButton2).addGap(52, 52, 52).addComponent(jButton3)
                        .addGap(47, 47, 47).addComponent(jButton4).addGap(58, 58, 58).addComponent(jButton5)
                        .addContainerGap(96, Short.MAX_VALUE)));

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
                new java.awt.Component[] { jButton1, jButton2, jButton3, jButton4, jButton5 });

        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton2).addComponent(jButton3).addComponent(jButton4)
                                .addComponent(jButton5))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 51,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL,
                new java.awt.Component[] { jButton1, jButton2, jButton3, jButton4, jButton5 });

        jPanel2.setLayout(new java.awt.BorderLayout());

        ta.setColumns(20);
        ta.setRows(5);
        jScrollPane1.setViewportView(ta);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 403,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(34, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        // 3) factory  SqlSession? 
        SqlSession ss = factory.openSession(true);

        // 4) sql  
        List<MemVO> lists = ss.selectList("mem.total");

        // 5) 
        StringBuffer sb = new StringBuffer("?\t?\t??\t?\t??\n"
                + "--------------------------------------------------------------"
                + "--------------------------------------------------------------");

        System.out.println("lists :" + lists.size());

        viewData(lists);
        ss.close();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void viewData(List<MemVO> lists) {

        StringBuffer sb = new StringBuffer("?\t?\t??\t?\t??\n"
                + "--------------------------------------------------------------"
                + "--------------------------------------------------------------");

        for (MemVO vo : lists) {

            // private String id, name, email, phone, reg_date;

            String id = vo.getId();
            String name = vo.getName();
            String email = vo.getEmail();
            String phone = vo.getPhone();
            String reg_date = vo.getReg_date();

            sb.append("\n");
            sb.append(id);
            sb.append("\t");
            sb.append(name);
            sb.append("\t");
            sb.append(email);
            sb.append("\t");
            sb.append(phone);
            sb.append("\t");
            sb.append(reg_date);
        }

        ta.setText(sb.toString());
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        //? ?
        String sname = JOptionPane.showInputDialog("?:");
        System.out.println("?:" + sname);

        // 3) factory  SqlSession? 
        SqlSession ss = factory.openSession(true);

        // 4) sql  
        List<MemVO> lists = ss.selectList("mem.searchName", sname);

        System.out.println("lists :" + lists.size());

        if (lists.size() > 0) {
            viewData(lists);
        } else {
            ta.setText("");
            JOptionPane.showMessageDialog(this, "? ? .");

        }

        ss.close();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        // ? UI

        new AddDialog(this, true);
        //dialog.setVisible(true);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        // ?  UI
        new UpdateDialog(this, true);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MyJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea ta;
    // End of variables declaration//GEN-END:variables

    void addData(String s_id, String s_pwd, String s_name, String s_email, String s_phone) {

        Map<String, String> map = new HashMap<String, String>();

        map.put("id", s_id);
        map.put("pwd", s_pwd);
        map.put("name", s_name);
        map.put("email", s_email);
        map.put("phone", s_phone);
        //5? ?  map? key -> mybatis 

        /*
                MemVO mem = new MemVO();
                mem.setId(s_id);
                mem.setName(s_name);
                mem.setEmail(s_email);
                mem.setPhone(s_phone);
        */
        // 3) factory  SqlSession? 
        SqlSession ss = factory.openSession(true);

        // 4) sql  
        int cnt = ss.insert("mem.add", map);

        if (cnt == 1) {
            JOptionPane.showMessageDialog(this, "??.");
        } else {
            JOptionPane.showMessageDialog(this, " ");
        }

    }

    public MemVO searchid(String s_id) {

        // 3) factory  SqlSession? 
        SqlSession ss = factory.openSession(true);

        // 4) sql          
        MemVO list = ss.selectOne("mem.searchId", s_id);

        return list;

    }

    public void updateData(String s_id, String s_pwd, String s_name, String s_email, String s_phone) {

        Map<String, String> map = new HashMap<String, String>();

        map.put("id", s_id);
        //map.put("pwd", s_pwd);
        map.put("name", s_name);
        map.put("email", s_email);
        map.put("phone", s_phone);

        SqlSession ss = factory.openSession(true);
        int cnt = ss.update("mem.update", map);

        if (cnt == 1) {
            JOptionPane.showMessageDialog(this, "?? ?.");
            ss.commit();
        } else {
            JOptionPane.showMessageDialog(this, " ");
            ss.rollback();
        }
        ss.close();
    }
}