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
'Web develop > JDBC_XML' 카테고리의 다른 글
[JDBC] Properties 사용법 및 사용예시 (0) | 2019.07.10 |
---|---|
[JDBC] JDBC 프로그래밍 (0) | 2019.07.10 |
[JDBC] Prepared Statement 사용법 및 사용예제 (0) | 2019.06.19 |
[JDBC] Properties MVC 사용예제 (+Swing) (0) | 2019.06.18 |
[JDBC] Properties 객체 사용방법(저장, 출력, 데이터얻기) (0) | 2019.06.18 |