반응형
1. JDBC 드라이버를 로딩
- Class.forName(“orale.jdbc.driver.OracleDriver”)
2. Connection 객체를 생성
- conn = DriverManager.getConnection(url, id, pw)
3. PreparedStatement 객체 생성, 객체 생성시 SQL 저장
- PreparedStaement - SQL문을 데이터베이스에 보내기위한 객체입니다.
- pstmt = conn.preparedStatement(sql)
4. pstmt.set<데이터타입>(? 순서, 값)
- ? 매개변수에 값을 지정합니다.
- ex) pstmt.setString(1, 값), pstmt.setInt(2, 값)
5. SQL 문장을 실행 후 결과를 리턴
- SQL 문장 실행 후, 변경된 row 수를 int type 으로 리턴합니다.
- pstmt.executeUpdate()
6. close
- ResultSet close
- PreparedStatement close
- Connection close
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
|
package oracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class OracleInsertTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
String url ="jdbc:oracle:thin:@localhost:1521:orcl";
String id = "aaaa4444";
String pw = "aaaa4444";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, id, pw);
conn.setAutoCommit(false);
String sql = "INSERT INTO wex001m VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"홍길동");
pstmt.setString(2, "16");
pstmt.setString(3, "서울시 서초동");
pstmt.setString(4, "010-1234-5678");
pstmt.setString(5, "abc@ddd.com");
pstmt.executeUpdate();
System.out.println("success");
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {pstmt.close();}
}catch(Exception e) {
e.printStackTrace();
}
try {
if(conn!=null) {conn.close();}
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
|
cs |
반응형