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:@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;
}
}
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
|
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 |
SelectTest
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import db.DBClose;
import db.DBConnection;
import dto.userDto;
public class SelectTest {
// 1개의 데이터만을 취득
// select는 1개의 데이터만을 취득하거나
// 데이터 전부를 취득
// 크게 2가지로 나눌수 있다.
// 1개의 데이터(row)만을 취득
//statement 사용
public userDto search(String id) {
String sql = " SELECT ID, NAME, AGE, JOINDATE"
+ " FROM USERDTO "
+ " WHERE ID = '" + id + "'";
Connection conn = DBConnection.getConnection();
PreparedStatement psmt = null;
ResultSet rs = null;
userDto dto = null;
System.out.println("sql: " + sql);
try {
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
// 현재 rs부분의 데이터가 있는 경우
if(rs.next()) {
String _id = rs.getString("id"); //SELECT ID
String _name = rs.getString("name"); // SELECT NAME
String _joindate = rs.getString("joindate");
dto = new userDto(_id, _name, _age, _joindate);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
return dto;
}
//PreparedStatement 사용하기
//PreparedStatement를 사용하면 ? 로 대체가 가능하다
public userDto select(String id) {
String sql = " SELECT ID, NAME, AGE, JOINDATE "
+ " FROM USERDTO "
+ " WHERE ID = ? ";
Connection conn = DBConnection.getConnection();
PreparedStatement psmt = null;
ResultSet rs = null;
userDto dto = null;
try {
psmt = conn.prepareStatement(sql);
psmt.setString(1, id); // 위의 ? 에 대입된다
rs = psmt.executeQuery();
if(rs.next()) {
dto = new userDto();
dto.setId(rs.getString("id"));
dto.setName(rs.getString("name"));
dto.setAge(rs.getInt("age"));
dto.setJoindate(rs.getString("joindate"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
return dto;
}
// 데이터 다수의 데이터 취득
public List<userDto> getUserList(){
String sql = " SELECT ID, NAME, AGE, JOINDATE "
+ " FROM USERDTO ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<userDto> list = new ArrayList<userDto>();
System.out.println("sql: " + sql);
try {
conn = DBConnection.getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String joindate = rs.getString("joindate");
list.add(new userDto(id, name, age, joindate));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
return list;
}
}
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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package selectTest;
import java.util.List;
import db.DBConnection;
import dto.userDto;
import jdbc.SelectTest;
public class mainClass {
public static void main(String[] args) {
DBConnection.initConnection();
SelectTest st = new SelectTest();
String id = "ccc";
userDto dto = st.search(id);
if(dto != null) {
System.out.println(dto.toString());
}else {
System.out.println("id를 찾을 수 없습니다");
}
id = "aaa";
dto = st.select(id);
if(dto != null) {
System.out.println(dto.toString());
}else {
System.out.println("id를 찾을 수 없습니다");
}
List<userDto> list = st.getUserList();
System.out.println(list.get(i).toString());
}
}
}
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 update(DBConnection, DBClose 활용) (0) | 2019.12.23 |
jdbc delete (0) | 2019.12.23 |
jdbc Insert (0) | 2019.12.23 |
jdbc eclipse기본 설정하기 (0) | 2019.12.23 |