package com.chocolleto.board.
user
;
import java.sql.
Connection
;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import com.chocolleto.board.db.DBUtils;
import com.chocolleto.board.
user
.model.
User
;
/**
* @author Administrator
*
*/
public
class UserDAO {
private DBUtils db;
// private Logger log = Logger.getLogger(UserDAO.class);
public
UserDAO() throws Exception {
// DB를 관리하는 객체의 인스턴스를 얻어온다.
db = DBUtils.getInstance();
}
/**
* 사용자 등록
*
* @param
user
* @throws SQLException
*/
public
void insertUser(
User
user
) throws SQLException {
String sql =
"insert into users(userid, password, nickname, birthday, email)"
+
"values(?,?,?,?,?)"
;
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy-MM-dd"
);
String birth;
if (
user
.getBirthday() ==
null
) {
birth =
null
;
}
else
{
birth = dateformat.format(
user
.getBirthday());
}
Connection
con =
null
;
try {
con = db.getConnection();
PreparedStatement st = con.prepareStatement(sql);
st.setString(1,
user
.getUserid());
st.setString(2,
user
.getPassword());
st.setString(3,
user
.getNickname());
st.setString(4, birth);
st.setString(5,
user
.getEmail());
st.executeUpdate();
st.
close
();
} catch (SQLException e) {
throw e;
} finally {
if (con !=
null
) {
try {
// 컨넥션은 익셉션이 발생하더라도 항상
close
() 될수 있도록 finally 에서 처리한다
con.
close
(); // 실제 컨넥션을
close
하는게 아니라 가지고 있던 MySQL의 컨넥션을 풀에 반환.
} catch (SQLException e) {
// 컨넥션을 닫을때 생기는 문제는 무시한다
}
}
}
}
/**
* 사용자 정보 수정
*
* @param
user
* @throws SQLException
*/
public
void updateUser(
User
user
) throws SQLException {
String sql =
"update users set password=?,nickname=?,birthday=?,email=?"
+
" where userid=?"
;
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy-MM-dd"
);
String birth;
if (
user
.getBirthday() ==
null
) {
birth =
null
;
}
else
{
birth = dateformat.format(
user
.getBirthday());
}
Connection
con =
null
;
try {
con = db.getConnection();
PreparedStatement st = con.prepareStatement(sql);
st.setString(1,
user
.getPassword());
st.setString(2,
user
.getNickname());
st.setString(3, birth);
st.setString(4,
user
.getEmail());
st.setString(5,
user
.getUserid());
st.executeUpdate();
st.
close
();
} catch (SQLException e) {
throw e;
} finally {
if (con !=
null
) {
try {
// 컨넥션은 익셉션이 발생하더라도 항상
close
() 될수 있도록 finally 에서 처리한다
con.
close
();
} catch (SQLException e) {
// 컨넥션을 닫을때 생기는 문제는 무시한다
}
}
}
}
/**
* 사용자 삭제
*
* @param userid
* @throws SQLException
*/
public
void deleteUser(String userid) throws SQLException {
String sql =
"delete from users where userid=?"
;
Connection
con =
null
;
try {
con = db.getConnection();
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, userid);
st.executeUpdate();
st.
close
();
} catch (SQLException e) {
throw e;
} finally {
if (con !=
null
) {
try {
// 컨넥션은 익셉션이 발생하더라도 항상
close
() 될수 있도록 finally 에서 처리한다
con.
close
();
} catch (SQLException e) {
// 컨넥션을 닫을때 생기는 문제는 무시한다
}
}
}
}
/**
* 사용자 검색.
*
* @param userid
* @param
password
* @throws SQLException
*/
public
User
selectUser(String id) throws SQLException {
String sql =
"select * from users where userid=?"
;
Connection
con =
null
;
User
user
=
null
;
try {
con = db.getConnection();
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, id);
ResultSet rs = st.executeQuery();
if (!rs.
next
()) {
// 검색된 내용이 없으면 회원이 아니므로
null
을 반환.
rs.
close
();
st.
close
();
}
else
{
// 검색된 내용이 있으면 검색된 내용을
user
에 저장.
user
= new
User
();
user
.setUserid(rs.getString(
"userid"
));
user
.setPassword(rs.getString(
"password"
));
user
.setNickname(rs.getString(
"nickname"
));
user
.setBirthday(rs.getDate(
"birthday"
));
user
.setEmail(rs.getString(
"email"
));
}
rs.
close
();
st.
close
();
} catch (SQLException e) {
throw e;
} finally {
if (con !=
null
) {
try {
// 컨넥션은 익셉션이 발생하더라도 항상
close
() 될수 있도록 finally 에서 처리한다
con.
close
();
} catch (SQLException e) {
// 컨넥션을 닫을때 생기는 문제는 무시한다
}
}
}
return
user
;
}
}