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
|
package orcale_jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class insertTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("이름 나이 주소");
String name = sc.nextLine();
int age = Integer.parseInt(sc.nextLine());
String addr = 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(" insert into member1(no,name,age,addr) ");
sql.append(" values(member_seq.nextval,?,?,?) ");
//일단 무슨값인지 모르므로 물음표로 채워두기
//아래에서 값을 채움
/*
insert into member(id, name, hiredate, age)
values(seq.nextval, ?, sysdate, ?);
pstmt.setString(1,'hong'); //첫번째 물음표
pstmt.setInt(2, 23); //두번째 물음표
*/
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(url, user, pwd);
pstmt = conn.prepareStatement(sql.toString());
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setString(3, addr);
// pstmt.setString(1, "엄수빈");
// pstmt.setInt(2, 49);
// pstmt.setString(3, "서울");
int r=pstmt.executeUpdate();
if(r>0)
System.out.println("자료 입력 성공");
else
System.out.println("자료 입력 실패");
// System.out.println("연결 성공");
}catch(ClassNotFoundException e) {
System.out.println(e);
}catch(SQLException e) {
System.out.println(e);
}finally {
if(pstmt!=null) try {pstmt.close();} catch(SQLException e) {}
if(conn!=null) try {conn.close();} catch(SQLException e) {}
}
}
}
|
cs |
반응형
'2019 > JDBC' 카테고리의 다른 글
JDBC Select (0) | 2019.11.26 |
---|---|
JDBC delete (0) | 2019.11.26 |
JDBC update (0) | 2019.11.26 |