hahahia

JSP, DB연동을 이용한 회원리스트 구현(계정삭제) 본문

Web Programming/JSP

JSP, DB연동을 이용한 회원리스트 구현(계정삭제)

hahahia 2012. 5. 26. 18:52


/* list.jsp 일부분(전체 소스는 여기로) */ 


<table border="1" cellspacing="0">

<tr>

<td>Number</td>

<td>ID</td>

<td>Name</td>

<td>password</td>


<TD>비고 </TD>


</tr>

<%

    while(rs.next()) {

%><tr>

<td><%= rs.getInt("idx") /*ⓖ*/%></td>

<td><%=rs.getString(2)%></td>

<td><%= rs.getString(3) /*ⓗ*/%></td>

<td><%=rs.getString("pwd")%></td>


<TD>

<A href="delete-do.jsp?idx=<%=rs.getInt("idx")%>">삭제</A>

<INPUT type="button" value="수정"

onClick="location.href='modify.jsp?idx=<%=rs.getInt("idx")%>'">

</TD>


</tr>

<%

    } // end while

%></table>



/* delete-do.jsp */


<%@ page contentType="text/html;charset=utf-8"

import="java.sql.*" %>

<%

request.setCharacterEncoding("utf-8");

String idx = request.getParameter("idx");

try

{

Class.forName("com.mysql.jdbc.Driver");

String DB_URL = "jdbc:mysql://localhost:3306/web01?useUnicode=true&characterEncoding=utf8";

Connection con = DriverManager.getConnection(DB_URL, "admin", "1234");

String sql = "DELETE FROM member WHERE idx=?";

PreparedStatement pstmt = con.prepareStatement(sql); // 쿼리를 이용해 delete 실행!!

pstmt.setInt(1,Integer.parseInt(idx));

pstmt.executeUpdate();

pstmt.close();

con.close();

}

catch(ClassNotFoundException e)

{

out.println(e);

}

catch(SQLException e)

{

out.println(e);

}

response.sendRedirect("list.jsp");

%>


실행결과



삭제라고 써져있는 링크를 눌러보도록 하겠습니다



해당 데이터가 삭제가된걸 볼 수 있네요



MYSQL로 확인해본 TABLE




Comments