본문 바로가기
2019/JDBC

JDBC delete

by SOLYI 2019. 11. 26.
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
56
57
58
59
60
61
package orcale_jdbc;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
 
public class deleteTest {
 
    public static void main(String[] args) {
        /*        1. class.forname
         *         2. Connection
         *         3. PreparedStatment
         *      4. sql문
         *      5. executeUpdate - insert, update, delete
         *                       - ddl
         *         executeQuery  - Select
         *         
         *        delete from 테이블명
         *        where 조건;
        */    
        
        
        Scanner sc = new Scanner(System.in);
        System.out.println("삭제할 번호 입력하세요");
        int num = Integer.parseInt(sc.nextLine());
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String user = "scott";
        String pwd = "tiger";
        
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from member1 ");
        sql.append(" where no = ?         ");
        
        try {
            Class C = Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection(url, user, pwd);
            pstmt = conn.prepareStatement(sql.toString());
            pstmt.setInt(1, num);
            
            int r = pstmt.executeUpdate();
            if(r>0)
                System.out.println("삭제 완료");
            else
                System.out.println("삭제 실패");
            
        }catch(Exception e) {
            System.out.println(e);
        }finally{
            if(pstmt!=nulltry { pstmt.close(); } catch(Exception e) {}
            if(conn!=nulltry { conn.close(); } catch(Exception e) {}
            
        }
    }
 
}
 
cs
반응형

'2019 > JDBC' 카테고리의 다른 글

JDBC Select  (0) 2019.11.26
JDBC update  (0) 2019.11.26
JDBC insert  (0) 2019.11.26