본문 바로가기

Java/jdbc

jdbc update(DBConnection, DBClose 활용)

반응형

DBConnection

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
package db;
 
 
public class DBConnection {
    public static void initConnection() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
 
            System.out.println("Driver Loading Success! ");
 
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() {
 
        Connection conn = null;
 
//        New Oracle : 우클릭 프로퍼티 -> 드라이버 프로퍼티 -> Connection Url 복사 
        try {
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:xe""hr""hr");
 
            System.out.println("DB Connection Success");
 
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return conn;
 
    }
}
 
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

 


DBClose

 

 

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
package db;
 
 
 
public class DBClose {
    public static void close(PreparedStatement psmt, Connection conn, ResultSet rs) {
 
        try {
            if (psmt != null) {
                psmt.close();
            }
            if(conn != null) {
                conn.close();
            }
            if(rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
    }
}
 
 
 
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

 

Update 구문 

 

 

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
package jdbc;
 
 
import db.DBClose;
import db.DBConnection;
 
public class UpdateTest {
    public boolean Update(String id, int age) {
        String sql = " UPDATE USERDTO "
                + " SET AGE = " + age + " "
                        + " WHERE ID = '"+ id + "'";
        Connection conn = DBConnection.getConnection();
        PreparedStatement stmt = null;
        
        int count = 0;
        
        System.out.println("sql :" + sql);
        
        try {
            stmt = conn.prepareStatement(sql);
            
            count = stmt.executeUpdate();
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBClose.close(stmt, conn, null);
        }
        return count > 0 ? true : false;
                
    }
}
 
 
 
 
 
 
 
 
 
 
 
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

 

메인 구문

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main;
 
import db.DBConnection;
import jdbc.UpdateTest;
 
public class mainClass {
    
    public static void main(String[] args) {
        DBConnection.initConnection();
        
        UpdateTest ut = new UpdateTest();
        String id = "bbb";
        int age = 23;
        
        boolean b = ut.Update(id, age);
        if(b) {
            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' 카테고리의 다른 글

일괄 삭제 하기  (0) 2020.01.21
jdbc Select  (0) 2019.12.24
jdbc delete  (0) 2019.12.23
jdbc Insert  (0) 2019.12.23
jdbc eclipse기본 설정하기  (0) 2019.12.23