본문 바로가기

Java/jdbc

jdbc delete

반응형

JDBC Test

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
package jdbc;
 
 
public class JdbcTest {
    public JdbcTest() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
 
            System.out.println("Driver Loading Success! ");
 
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
//    실제 연결 코드
    public Connection getConnection() {
 
        Connection conn = null;
 
//        New Oracle : 우클릭 프로퍼티 -> 드라이버 프로퍼티 -> Connection Url 복사 
        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.2.5:1521:xe""hr""hr");
 
            System.out.println("DB Connection Success");
 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return conn;
    }
 
    public int Delete(String id) {
        String sql = " DELETE FROM USERDTO"
                + " WHERE ID = '" + id + "'";
        
        Connection conn = getConnection();
        Statement stmt = null;
        
        int count = 0;
        
        System.out.println("sql: " + sql);
        
        try {
            stmt = conn.createStatement();
            
            count = stmt.executeUpdate(sql);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            
            try {
                if(stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return count;
    }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

main 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main;
 
import jdbc.JdbcTest;
 
public class mainClass {
    public static void main(String[] args) {
        JdbcTest jt = new JdbcTest();
        
        String id = "aaa";
 
        int count = jt.Delete(id);
        if(count > 0) {
            System.out.println("성공적으로 삭제하였습니다.");
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

반응형

'Java > jdbc' 카테고리의 다른 글

jdbc Select  (0) 2019.12.24
jdbc update(DBConnection, DBClose 활용)  (0) 2019.12.23
jdbc Insert  (0) 2019.12.23
jdbc eclipse기본 설정하기  (0) 2019.12.23
DBConnection, DBClose 만들어두기  (0) 2019.12.23