ResultSet : select문에서만 사용하는 라이브러리 / 변수.next() 로 발동 필수 배열형태(db 전용배열) + bufferReader 기능 포함 getString,getInt,getArary...등 : 데이터베이스에 맞는 자료형을 가져올 때 사용 (getint시 1부터 : 0은 다른거임..)
public class loginok extends HttpServlet {
private static final long serialVersionUID = 1L;
dbconfig db = new dbconfig();
Connection con = null;
PrintWriter pw = null;
Statement st = null;
HttpSession hs = null; //session
//로그인파트
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String uid = request.getParameter("uid");
String upass = request.getParameter("upass");
try {
this.con= this.db.info();
String sql = "select * from user where uid='"+uid+"' and upass='"+upass+"'";
this.st = this.con.createStatement();
ResultSet rs = this.st.executeQuery(sql); //ResultSet : select문에서만 사용하는 라이브러리
//rs.next(); //rs 1번은 발동시켜줘야함 (=buffer같은거)
//System.out.println (rs.getString("uname")); //로그인 성공시 username sysout
this.pw = response.getWriter();
String mid=""; //session에 저장할 id
String mname=""; //session에 저장할 이름
while(rs.next()) {
mid = rs.getString("uid");
//System.out.println(rs.getDate(5)); //timestamp는 getdate or getstring으로 받음
mname = rs.getString("uname");
}
//=> 로그인페이지라 반복문에 넣을필요 x,게시판등 여러번 가져올거라면 반복문에 넣어야함(배열이기때문)
if(mname.equals("")) { //select의 값이 없을 경우
this.pw.write("<script>"
+ "alert('아이디 및 비밀번호를 확인하세요!');"
+ "history.go(-1);"
+ "</script>");
}else { //해당 sql쿼리가 정상 작동하여 값을 로드하였을 경우
this.hs = request.getSession();
this.hs.setAttribute("id",mid);
this.hs.setAttribute("name",mname);
this.pw.write("<script>"
+ "alert('로그인 성공!');"
+ "location.href = './top.jsp';"
+ "</script>");
}
this.pw.close();
rs.close();
}catch(Exception e) {
System.out.println(e);
System.out.println("db연결실패!!");
try {
this.st.close();
this.con.close();
}catch(Exception e2) {
System.out.println("db 해제 실패 !");
}
}
}
}