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
|
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 updateTest {
public static void main(String[] args) {
/* 1. class.forname
* 2. Connection
* 3. PreparedStatment
* 4. sql문
* 5. executeUpdate - insert, update, delete
* - ddl
* executeQuery - Select
*
* update 테이블명
* set 컬럼명 = 바꿀값,
* 컬럼명 = 바꿀값
* where 조건;
*/
Scanner sc = new Scanner(System.in);
System.out.println("변경할 번호, 새로운 이름과 나이");
int num = Integer.parseInt(sc.nextLine());
String name = sc.nextLine();
int age = Integer.parseInt(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(" update member1 ");
sql.append(" set name = ? , age = ? ");
sql.append(" where no = ? ");
try {
Class c = 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.setInt(3, num);
int r=pstmt.executeUpdate();
if(r>0)
System.out.println("자료 변경 성공");
else
System.out.println("자료 변경 실패");
}catch(Exception 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 insert (0) | 2019.11.26 |