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;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
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;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBClose {
public static void close(PreparedStatement psmt, Connection conn, ResultSet rs) {
try {
if (psmt != null) {
}
if(conn != null) {
}
if(rs != null) {
}
} 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 java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
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;
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 |