본문 바로가기

Java/jdbc

jdbc Insert

반응형

JDBC(Insert 선언 쪽)

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
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 Insert(String id, String name, int age) {
        // createStatement, preperedStatement
 
        String sql = " INSERT INTO USERDTO(ID, NAME, AGE, JOINDATE) " + " VALUES('" + id + "', '" + name + "', " + age
                + ", SYSDATE) ";
        Connection conn = this.getConnection();
        Statement stmt = null;
 
        int count = 0// 데이터가 몇개 변경되었는지 확인 변수
 
        System.out.println("sql:" + sql);
 
        try {
            stmt = conn.createStatement(); // 연결부분에 대한 상태를 생성해 준다.
 
            count = stmt.executeUpdate(sql);
 
            System.out.println("성공적으로 추가되었습니다");
 
        } 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
 

 

메인

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main;
 
import jdbc.JdbcTest;
 
public class mainClass {
 
    public static void main(String[] args) {
        
        JdbcTest jt = new JdbcTest();
        
        int count = jt.Insert("bbb", "일지매"21);
        
        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 delete  (0) 2019.12.23
jdbc eclipse기본 설정하기  (0) 2019.12.23
DBConnection, DBClose 만들어두기  (0) 2019.12.23