일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- SUB함수
- ctrl+/
- is_check
- calc.minus
- selectedIndex
- push오류
- addClass
- slideUp
- calc.plus
- FileZilla다운로드
- excel중복체크
- selectoptions
- Math.floor()
- Math.ceil()
- index %
- 파일질라다운로드
- Git
- 파일질라설치
- Excel
- removeClass
- 증가값
- 주석이 먹히지 않을 때
- 1521
- 파일질라설치오류
- hide
- toFixed()
- Math.round()
- FileZilla설치
- Parent
- 소스트리인증실패
- Today
- Total
잡동사니에도 사랑을
[21.08.17] 홈, 회원가입, 로그인 화면 만들기 - DB연동(Main,Panel,DTO, DAO) 본문
[21.08.17] 홈, 회원가입, 로그인 화면 만들기 - DB연동(Main,Panel,DTO, DAO)
luvforjunk 2021. 8. 24. 23:01[문제 - 패널을 이용한 회원가입 프로그램 만들기]
////////MemberDTO
package dbtest;
// 데이터만 보관 - 1인분 (Data Transfer Object = VO:Value Object)
public class MemberDTO {
private String name, id, pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
////////MemberMain
package dbtest;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Frame-BorderLayout(동서남북), 동서남북 각각 1개의 Component가 올라간다
public class MemberMain extends Frame implements ActionListener {
private Button homeBtn, writeBtn, loginBtn;
private CardLayout card;
private Panel centerP;
public MemberMain( ) {
homeBtn = new Button("홈");
writeBtn = new Button("회원가입");
loginBtn = new Button("로그인");
card = new CardLayout( );
// 3개의 버튼
Panel northP = new Panel( ); // Panel-FlowLayout(순서배치)
northP.setBackground(new Color(45, 180, 0));
northP.add(homeBtn);
northP.add(writeBtn);
northP.add(loginBtn);
// 카드레이아웃으로 깬 패널 입니다 여기가 계속 바뀔겁니다
centerP = new Panel();
centerP.setLayout(card);
centerP.add("home", new HomePanel());
centerP.add("write", new WritePanel());
centerP.add("login", new LoginPanel());
this.add("North", northP);
this.add("Center", centerP);
this.setBounds(700, 100, 500, 500);
this.setResizable(false);
this.setVisible(true);
// 이벤트
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 버튼 이벤트
homeBtn.addActionListener(this);
writeBtn.addActionListener(this);
loginBtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == homeBtn) {
card.show(centerP, "home");
} else if (e.getSource() == writeBtn) {
card.show(centerP, "write");
} else if (e.getSource() == loginBtn) {
card.show(centerP, "login");
}
}
public static void main(String[] args) {
new MemberMain();
}
}
////////HomePanel
package dbtest;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
public class HomePanel extends Panel{
public HomePanel() {
// s.setBackground(Color.RED);
}
@Override
public void paint(Graphics g) {
Toolkit t = Toolkit.getDefaultToolkit();
Image img = t.getImage("갓연경.jpg");
g.drawImage(img, this.getWidth(), this.getHeight(), this);
}
public static void main(String[] args) {
new HomePanel();
}
}
////////WritePanel
package dbtest;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class WritePanel extends Panel {
private Label nameL, idL, pwdL;
private TextField nameT, idT, pwdT;
private Button writeBtn, resetBtn;
public WritePanel() {
this.setBackground(Color.GREEN);
setLayout(null);
// 생성
nameL = new Label("이름");
idL = new Label("아이디");
pwdL = new Label("비밀번호");
nameT = new TextField();
idT = new TextField();
pwdT = new TextField();
writeBtn = new Button("등록");
resetBtn = new Button("다시 작성");
// 글꼴 크기
Font labelF = new Font("맑은고딕", 1, 15);
Font textF = new Font("맑은고딕", 1, 25);
nameL.setFont(labelF);
nameT.setFont(textF);
idL.setFont(labelF);
idT.setFont(textF);
pwdL.setFont(labelF);
pwdT.setFont(textF);
writeBtn.setFont(labelF);
resetBtn.setFont(labelF);
// 배치
nameL.setBounds(20, 30, 70, 30);
nameT.setBounds(20, 60, 450, 50);
idL.setBounds(20, 120, 70, 30);
idT.setBounds(20, 150, 450, 50);
pwdL.setBounds(20, 210, 70, 30);
pwdT.setBounds(20, 240, 450, 50);
writeBtn.setBounds(20, 350, 220, 50);
resetBtn.setBounds(250, 350, 220, 50);
this.add(nameL);
this.add(nameT);
this.add(idL);
this.add(idT);
this.add(pwdL);
this.add(pwdT);
this.add(writeBtn);
this.add(resetBtn);
// 이벤트
writeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 데이터
String name = nameT.getText();
String id = idT.getText();
String pwd = pwdT.getText();
MemberDTO dto = new MemberDTO();
dto.setName(name);
dto.setId(id);
dto.setPwd(pwd);
// DB
MemberDAO dao = new MemberDAO();
int su = dao.insertMember(dto);
JOptionPane.showMessageDialog(null, "회원가입 되었습니다");
}
});
resetBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
nameT.setText("");
idT.setText("");
pwdT.setText("");
}
});
}
}
////////LoginPanel
package dbtest;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class LoginPanel extends Panel {
private Label idL, pwdL;
private TextField idT, pwdT;
private Button loginBtn, resetBtn;
public LoginPanel() {
this.setBackground(Color.GREEN);
setLayout(null);
// 생성
idL = new Label("아이디");
pwdL = new Label("비밀번호");
idT = new TextField();
pwdT = new TextField();
loginBtn = new Button("로그인");
resetBtn = new Button("다시 작성");
// 글꼴 크기
Font labelF = new Font("맑은고딕", 1, 15);
Font textF = new Font("맑은고딕", 1, 25);
idL.setFont(labelF);
idT.setFont(textF);
pwdL.setFont(labelF);
pwdT.setFont(textF);
loginBtn.setFont(labelF);
resetBtn.setFont(labelF);
// 배치
idL.setBounds(20, 100, 70, 30);
idT.setBounds(20, 130, 450, 50);
pwdL.setBounds(20, 190, 70, 30);
pwdT.setBounds(20, 220, 450, 50);
loginBtn.setBounds(20, 330, 220, 50);
resetBtn.setBounds(250, 330, 220, 50);
this.add(idL);
this.add(idT);
this.add(pwdL);
this.add(pwdT);
this.add(loginBtn);
this.add(resetBtn);
// 이벤트
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 데이터
String id = idT.getText();
String pwd = pwdT.getText();
// DB
MemberDAO dao = new MemberDAO();
String name = dao.loginMember(id, pwd);
if (name == null) {
JOptionPane.showMessageDialog(null, "아이디 또는 비밀번호가 틀립니다.");
} else {
JOptionPane.showMessageDialog(null, name + "님 로그인");
}
}
});
resetBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
idT.setText("");
pwdT.setText("");
}
});
}
}
////////MemberDAO
package dbtest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//DB
public class MemberDAO {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String username = "c##java";
private String password = "bit";
public MemberDAO( ) {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getConnection() {
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
public int insertMember(MemberDTO dto) {
String sql = "insert into member values(?, ?, ?)";
this.getConnection();
int su = 0;
try {
pstmt = conn.prepareStatement(sql); // 생성
pstmt.setString(1, dto.getName());
pstmt.setString(2, dto.getId());
pstmt.setString(3, dto.getPwd());
su = pstmt.executeUpdate();// 실행 - 개수 리턴
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return su;
}
public String loginMember(String id, String pwd) {
String name = null;
String sql = "select * from member where id=? and pwd=?";
getConnection();
try {
pstmt = conn.prepareStatement(sql); // 생성
pstmt.setString(1, id);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery(); // 실행
if (rs.next())
name = rs.getString("name");
// ID는 중복값이 안들어가므로 if문을 사용한다
// true면 name을 불러오고 false면 null값
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return name;
}
}