2019.02.07 JDBC: SQL문 사용하기 (DELETE)

2019. 2. 7. 10:42JSP

#1 SQL: DELETE(삭제)


< 삭제 전 location Table>





deleteForm.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>insertForm</title>
</head>
<body>
    <h1>도시 위치값 삭제</h1>
    
    <form action="deletePro.jsp"  method="post">
        도시이름: <input type ="text" name="cityName">
        <button type = "submit">삭제</button>
    </form>
 
</body>
</html> 
cs



deletePro.jsp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
     <%
         // post request 한글처리
         request.setCharacterEncoding("UTF-8");
     
         // 파라미터 정보 가져오기
         String cityName = request.getParameter("cityName");
         
         // 현재 시스템의 날짜 가져오기
         Timestamp reg_date = new Timestamp(System.currentTimeMillis()); //현재 시스템의 날짜 
         
         // DELETE를 위한 JDBC 변수 
         Connection con = null;
         PreparedStatement pstmt = null;
         
         // DB연결
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String user = "scott";
        String passwd = "tiger";
        Class.forName("oracle.jdbc.OracleDriver");
        con = DriverManager.getConnection(url, user, passwd);
        
        // SQL문 준비
        String sql = "DELETE FROM location WHERE city_name=?";
        
        pstmt = con.prepareStatement(sql); // sql문 전달
        pstmt.setString(1,cityName);
 
        // 데이터를 DB에 갱신
        pstmt.executeUpdate();
        %>
        <script>
        alert('삭제완료');
        location.href='deleteForm.jsp';
        </script>
        <% 
        pstmt.close();
        con.close();
        
         %>
 
</body>
</html>
cs




<실행 후 location Table>




'JSP' 카테고리의 다른 글

2019.02.18 JSTL - JSTL core  (0) 2019.02.18
2019.02.18 EL(Expression Langauage)  (0) 2019.02.18
2019.02.01 JDBC: SQL문 사용하기 (INSERT)  (0) 2019.02.01
2019.02.01 JDBC: SQL문 사용하기 (SELECT)  (0) 2019.02.01
2019.02.01 JDBC의 연동  (0) 2019.02.01