dao/MemberDAO.java > MODEL2

본문 바로가기

사이트 내 전체검색

뒤로가기 MODEL2

dao/MemberDAO.java

페이지 정보

작성자 관리자 작성일 24-04-09 15:00 조회 292 댓글 0

본문

package dao;


import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;


import model.Member;


public class MemberDAO {


//사용할 객체를 미리 선언

Statement stmt;

PreparedStatement pstmt;

ResultSet rs;

DAO d = new DAO(); //d.getCon() , d.conn 사용 목적

//아이디 존재 여부

public int loginSelect(String id) {

d.getCon();

int num = 0;

try {

String sql = "select count(*) from member where id=?";

pstmt = d.conn.prepareStatement(sql);

pstmt.setString(1, id);

rs = pstmt.executeQuery();

if(rs.next()) {

num = rs.getInt(1);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return num;

}

//회원가입

public void insertMember(Member member) {

d.getCon(); //데이터베이스 연결

try {

String sql = "insert into member values (?,?,?,?,?,?,?,?)";

pstmt = d.conn.prepareStatement(sql);

pstmt.setString(1, member.getId());

pstmt.setString(2, member.getPass());

pstmt.setString(3, member.getName());

pstmt.setInt(4, member.getAge());

pstmt.setString(5, member.getGender());

pstmt.setString(6, member.getEmail());

pstmt.setString(7, "1");

pstmt.setString(8, member.getMemo());

pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close(); //사용 객체 닫음

d.conn.close(); //사용 객체 닫음

}catch(Exception e) {

e.printStackTrace();

}

}

}

//로그인

public Member loginMemberSelect(String id, String pass) {

d.getCon();

Member m = new Member();

try {

String sql = "select * from member where id=? and pass=?";

pstmt = d.conn.prepareStatement(sql);

pstmt.setString(1, id);

pstmt.setString(2, pass);

rs = pstmt.executeQuery();

if(rs.next()) {

m.setId(rs.getString("id")); //id : 데이터베이스 테이블 칼럼명

m.setName(rs.getString("name"));

m.setLevel(rs.getString("level"));

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return m;

}

//회원 한명의 정보

public Member oneMember(String id) {

d.getCon();

Member m = new Member();

try {

String sql = "select * from member where id=?";

pstmt = d.conn.prepareStatement(sql);

pstmt.setString(1, id);

rs = pstmt.executeQuery();

if(rs.next()) {

m.setId(rs.getString("id")); //id : 데이터베이스 테이블 칼럼명

m.setPass(rs.getString("pass"));

m.setName(rs.getString("name"));

m.setAge(rs.getInt("age"));

m.setGender(rs.getString("gender"));

m.setEmail(rs.getString("email"));

m.setLevel(rs.getString("level"));

m.setMemo(rs.getString("memo"));

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return m;

}

//회원 수정

public void updateMember(Member m) {

d.getCon();

try {

String sql = "update member set pass=?,name=?,age=?,gender=?,email=?,memo=? where id=?";

pstmt = d.conn.prepareStatement(sql);

pstmt.setString(1, m.getPass());

pstmt.setString(2, m.getName());

pstmt.setInt(3, m.getAge());

pstmt.setString(4, m.getGender());

pstmt.setString(5, m.getEmail());

pstmt.setString(6, m.getMemo());

pstmt.setString(7, m.getId());

pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//회원가입 - 카카오, 네이버

public void insertMemberApi(String id) {

d.getCon();


try {

String sql = "insert into member (id,name,email,connecttype ) values (?,?,?,?)";

pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, id);

pstmt.setString(2, "카카오로그인");

pstmt.setString(3, id);

pstmt.setString(4, "카카오");


pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close(); //사용 객체 닫음

d.conn.close(); //사용 객체 닫음

}catch(Exception e) {

e.printStackTrace();

}

}

}

}


추천0

댓글목록 0

등록된 댓글이 없습니다.


Copyright © dancePKT. All rights reserved.
PC 버전으로 보기