Web develop/JDBC_XML

[JDBC] Swing에서 중복아이디 체크하기 (PrepareStatement 사용)

ForA 2019. 6. 19. 00:01
728x90

중복확인 JButton 클릭시

    -------View---------
    public JButton bt_checkId;
    public JTextField tf_id;

    --------Model(DAO)----------
    public boolean findExistID(String id)
    {
        connect();
        try {
            String sql = "SELECT count(*) cnt FROM membership WHERE id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                int cnt = rs.getInt("cnt");
                if (cnt > 0) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            disconnect();
        }
        return false;
    }//find

    --------Controller-------------
    MemberDAO dao = new MemberDAO();
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object ob = e.getSource();
        if(ob==joinForm.bt_checkId){
            if(dao.findExistId(joinForm.tf_id.getText())){ //이미 저장된 아이디 존재
                JOptionPane.showMessageDialog(joinForm,"사용중인아이디");
                joinForm.tf_id.setText("");
                return;
            }else {
                JOptionPane.showMessageDialog(joinForm,"사용가능아이디");
            }
        }
    }//actionPerformed

KeyListener 사용하여 중복체크

    -------View---------
    public JTextField tf_id;
    public JLabel la_id_check;

    tf_id = new JTextField();
    la_id_check = new JLabel();
    la_id_check.setText("사용가능");
    --------Model(DAO)----------
    public boolean findExistID(String id)
    { 
        connect();
        try {
            String sql = "SELECT count(*) cnt FROM membership WHERE id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                int cnt = rs.getInt("cnt");
                if (cnt > 0) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            disconnect();
        }
        return false;
    }//find

    --------Controller-------------
    MemberDAO dao = new MemberDAO();

    joinForm.tf_id.addKeyListener(new KeyAdapter(){//KeyAdapter: 인터페이스
        public void keyReleased(KeyEvent e){ //키를 떼었을때 (값을 얻어올 수 있음)
            MembershipDAO dao = new MembershipDAO();
            if(dao.findExistId(joinForm.tf_id.getText())){//중복된 아이디라면
                joinForm.la_id_check.setForegrounde(Color.RED);
                joinForm.la_id_check.setText("이미 사용");
            }else {
                joinForm.la_id_check.setForegrounde(Color.BLUE);
                joinForm.la_id_chekc.setText("사용 가능 아이디");
            }
      }
    });

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object ob = e.getSource();
        if(ob==joinForm.bt_checkId){
            if(dao.findExistId(joinForm.tf_id.getText())){ //이미 저장된 아이디 존재
                JOptionPane.showMessageDialog(joinForm,"사용중인아이디");
                joinForm.tf_id.setText("");
                return;
            }else {
                JOptionPane.showMessageDialog(joinForm,"사용가능아이디");
            }
        }
    }//actionPerformed