๊ณผ์ 2 : ์ฑ ๊ฒ์ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค Java ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ
์ด ํ๋ก๊ทธ๋จ์ Kakao Book Open API๋ฅผ ํ์ฉํ์ฌ ์ฑ ์ ๊ฒ์ํ๊ณ , ๊ฒ์ํ ์ฑ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
๐๊ณผ์ ์๋ด
- (๊ฐ์ฌ๋) ๊ณผ์ ์๋ด: 8์ 25์ผ(๊ธ)
- (์๊ฐ์) ๊ณผ์ ์ ์ถ: ~8์ 30์ผ(์) 23:59
- (๋ฉํ ์ง) ๊ณผ์ ์ฑ์ : 8์ 31์ผ(๋ชฉ) ~ 9์ 6์ผ(์)
โ๏ธ 1.๋ฌธ์ ์ค๋ช
๋จ๊ณ 1: Kakao API ํค ํ๋
- Kakao Developers์ ๋ก๊ทธ์ธํฉ๋๋ค.
- ๋ก๊ทธ์ธ ํ, ์์ ๊ฐ์ด๋์ ๋ฐ๋ผ ์์ ๋ง์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฑํฉ๋๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์์ฑ ์, ๊ฐ ํ๋ซํผ๋ณ๋ก ์ฑ ํค๊ฐ ๋ฐ๊ธ๋ฉ๋๋ค. ๋์ค ์ฌ์ฉ์ ์ํด REST API ํค๋ฅผ ๋ฉ๋ชจํด๋์ธ์.
๋จ๊ณ 2: ์ฑ ๊ฒ์ API ์ฌ์ฉ
- ๋ค์ ๊ฒ์ REST API ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ฑ ๊ฒ์ REST API๋ฅผ ํ์ธํฉ๋๋ค.
- ๋ฌธ์์์ ์ ๊ณต๋ ์ฑ ๊ฒ์ ์์ ์ฝ๋๋ฅผ ํ์ธํ์ฌ ์์ฒญ๊ณผ ์๋ต ๊ตฌ์กฐ๋ฅผ ์ดํดํฉ๋๋ค.
๋จ๊ณ 3: Java ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌํ
- Kakao Book Open API์ ์ํธ์์ฉํ๋ Java ์ ํ๋ฆฌ์ผ์ด์ ์ ์ค๊ณํฉ๋๋ค.
- ์ ๋ ฅ์ผ๋ก ์ฑ ์ ๋ชฉ์ ์ ๊ณตํ๊ณ API์์ ๋ฐ์์จ ์ฑ ๋ฐ์ดํฐ๋ฅผ JSON ํ์์ผ๋ก ํ์ฑํฉ๋๋ค.
- ๊ฒ์ํ ์ฑ ์ ๊ดํ ์ ๋ณด๋ฅผ ๋์ ์ ๋ชฉ, ๊ฐ๊ฒฉ, ์ถํ์ฌ, ์ ์, ํ ์ธ ๊ฐ๊ฒฉ ๋ฐ ISBN๊ณผ ๊ฐ์ด ์ ์ ํ ์ ๋ณด๋ก ์ถ๋ ฅํฉ๋๋ค.
- ์ฑ ๋ฐ์ดํฐ๋ ๊ธฐ๋ณธ 10๊ฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
๋จ๊ณ 4: ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฅ
- ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ ํ, ์ฌ์ฉ์์๊ฒ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ ์ง ์ฌ๋ถ๋ฅผ ์ ํํ๋๋ก ์๋ดํฉ๋๋ค.
- ์ฌ์ฉ์๊ฐ ์ ์ฅ์ ์ ํํ ๊ฒฝ์ฐ (Y), ๊ด๋ จ ์ฑ ์ ๋ณด๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๋ ๋ก์ง์ ๊ตฌํํฉ๋๋ค.
- ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋ ์ฑ ๋ชฉ๋ก์ ๋์ ์ ๋ชฉ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ์ฌ ๋ถ๋ฌ์ค๋ ๋ก์ง์ ๊ตฌํํฉ๋๋ค.
โ๏ธ 2.์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ์์
[์ ๋ ฅ ํ๋ฉด]
๋์๋ฅผ ๊ฒ์ํ ์ ๋ชฉ์ ์ ๋ ฅํ์ธ์: ์๋ฐ์ ์ ์
[์ถ๋ ฅ ๊ฒฐ๊ณผ]
๋์ ์ ๋ชฉ๊ฐ๊ฒฉ์ถํ์ฌ์๊ฐํ ์ธ ๊ฐ๊ฒฉISBN
์๋ฐ์ ํ์ค | 30000 | ๋์ธ | ๋จ๊ถ์ฑ | 26000 | 98210011 |
... |
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ์๊ฒ ์ต๋๊น? Y/N
์ ์ฅ์ฑ๊ณต
[TABLE LIST]
๋์ ์ ๋ชฉ๊ฐ๊ฒฉ์ถํ์ฌ์๊ฐํ ์ธ ๊ฐ๊ฒฉISBN
์๋ฐ์ ํ์ค | 30000 | ๋์ธ | ๋จ๊ถ์ฑ | 26000 | 98210011 |
... |
โ๏ธ 3.์์กด์ฑ
- Java 8 ์ด์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์: JDBC) ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํธ์์ฉ์ ์ํด ํ์
โ๏ธ 4.ํ๊ฐ๊ธฐ์ค
- ์ ์ถ ๊ธฐํ์ ๋ง์ถ์ด ์ ์ถํ์๋๊ฐ? (5์ )
- ์ค์ต ๋ฌธ์ ๋ณ ์ ์๋ ์กฐ๊ฑด์ ๋ง์ถ์ด ์ฝ๋๊ฐ ์์ฑ๋์๋๊ฐ? (5์ )
- ์ ์๋ ๊ธฐ๋ฅ๋ค์ด ์ ์์ ์ผ๋ก ๋์ํ๋๊ฐ? (5์ )
- ์ ์ถ๋ ฅ ํ๋ฉด์ด ์ ์ค๊ณ ๋์๋๊ฐ?
- JSON ์ฑ ๋ฐ์ดํฐ ์ ํ์ฑํ์ฌ ๋ชฉ๋ก์ ์ถ๋ ฅํ์๋๊ฐ?
- ์ฑ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ ์ฅ ํ์๋๊ฐ?
- ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋ ์ฑ ๋ชฉ๋ก์ ์ ๋ถ๋ฌ ์๋๊ฐ?
๐ฏKAKAO API๋ฅผ ์ฌ์ฉํ์ฌ ๋์ ๊ฒ์
๐์์กด์ฑ
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
ํจํค์ง ์ค๊ณ
Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
private String title;
private int price;
private String publisher;
private String author;
private int salePrice;
private String isbn;
}
|
cs |
GetFileInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.io.FileInputStream;
import java.util.Properties;
public class GetFileInfo {
public String getValue(String keyword) {
Properties properties = new Properties();
String info = null;
try {
properties.load(new FileInputStream("config.yml"));
info = properties.getProperty(keyword);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
}
|
cs |
MySQLBookDAO.java
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
|
import org.example.model.Book;
import org.example.service.GetFileInfo;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MysqlBookDAO implements BookRepository {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
public void getConnection() {
GetFileInfo getFileInfo = new GetFileInfo();
String url = getFileInfo.getValue("sql_url");
String username = getFileInfo.getValue("sql_username");
String password = getFileInfo.getValue("sql_password");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int saveBook(List<Book> bookList) {
String query = "insert into booktbl(title, price, publisher, author, saleprice, isbn) values(?, ?, ?, ?, ?, ?)";
int cnt = -1;
getConnection();
try {
ps = conn.prepareStatement(query);
for (int i = 0; i < bookList.size(); i++) {
ps.setString(1, bookList.get(i).getTitle());
ps.setInt(2, bookList.get(i).getPrice());
ps.setString(3, bookList.get(i).getPublisher());
ps.setString(4, bookList.get(i).getAuthor());
ps.setInt(5, bookList.get(i).getSalePrice());
ps.setString(6, bookList.get(i).getIsbn());
ps.executeUpdate();
}
cnt = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
dbClose();
}
return cnt;
}
@Override
public List<Book> findAll() {
String query = "select * from booktbl order by title asc";
getConnection();
List<Book> bookList = new ArrayList<>();
try {
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
while (rs.next()) {
String title = rs.getString("title");
int price = rs.getInt("price");
String publisher = rs.getString("publisher");
String author = rs.getString("author");
int salePrice = rs.getInt("saleprice");
String isbn = rs.getString("isbn");
Book book = new Book(title, price, publisher, author, salePrice, isbn);
bookList.add(book);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dbClose();
}
return bookList;
}
public void dbClose() {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
BookFindService.java
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
|
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.example.model.Book;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class BookFindService {
private static final String KAKAO_BOOK_API_URL = "https://dapi.kakao.com/v3/search/book?query=";
public List<Book> searchBook(String keyword) {
List<Book> bookList = null;
GetFileInfo getFileInfo = new GetFileInfo();
String kakaoRestApiKey = getFileInfo.getValue("KAKAO_REST_API_KEY");
try {
String query = keyword.replaceAll("\\s", "");
String requestURL = KAKAO_BOOK_API_URL + query;
HttpClient client = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(requestURL);
getRequest.addHeader("Authorization", "KakaoAK " + kakaoRestApiKey);
HttpResponse response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String jsonResponse = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(jsonResponse);
JSONArray documents = jsonObject.getJSONArray("documents");
if (!documents.isEmpty()) {
bookList = setBookDTO(documents);
}
} else {
System.out.println("response is error : " + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
return bookList;
}
public List<Book> setBookDTO(JSONArray documents) {
List<Book> bookList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
JSONObject document = documents.getJSONObject(i);
Book book = new Book();
book.setTitle(document.getString("title"));
JSONArray authorList = document.getJSONArray("authors");
String author = authorList.getString(0);
book.setAuthor(author);
book.setPublisher(document.getString("publisher"));
book.setPrice(document.getInt("price"));
book.setSalePrice(document.getInt("sale_price"));
book.setIsbn(document.getString("isbn"));
bookList.add(book);
}
return bookList;
}
}
|
cs |
Main.java
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
|
import org.example.model.Book;
import org.example.repository.MysqlBookDAO;
import org.example.service.BookFindService;
import java.util.List;
import java.util.Scanner;
public class Main {
static BookFindService bookFindService;
static MysqlBookDAO mysqlBookDAO;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("๋์๋ฅผ ๊ฒ์ํ ์ ๋ชฉ์ ์
๋ ฅํ์ธ์: ");
String keyword = scanner.next();
bookFindService = new BookFindService();
mysqlBookDAO = new MysqlBookDAO();
List<Book> bookList = bookFindService.searchBook(keyword);
if (bookList != null) {
printBookList(bookList);
check_saveDb(bookList);
} else {
System.out.println("๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.");
}
List<Book> foundBookList = mysqlBookDAO.findAll();
printBookList(foundBookList);
}
public static void check_saveDb(List<Book> bookList) {
System.out.println("๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ์๊ฒ ์ต๋๊น? Y/N");
boolean check = false;
while (!check) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if (input.equals("Y")) {
int cnt = mysqlBookDAO.saveBook(bookList);
if (cnt > 0) {
System.out.println("์ ์ฅ์ฑ๊ณต");
} else {
System.out.println("์ ์ฅ์คํจ");
}
check = true;
} else if (input.equals("N")) {
check = true;
} else {
System.out.println("Y ๋๋ N ์ค ํ๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์.");
}
}
}
public static void printBookList(List<Book> bookList) {
StringBuilder sb = new StringBuilder();
sb.append("[TABLE LIST]\n");
sb.append(String.format("%-80s | %-15s | %-15s| %-15s| %-15s| %-15s \n", "๋์์ ๋ชฉ", "๊ฐ๊ฒฉ", "์ถํ์ฌ", "์๊ฐ", "ํ ์ธ ๊ฐ๊ฒฉ", "ISBN"));
sb.append("=========================================================================================================================================================================\n");
for (Book book : bookList) {
sb.append(String.format("%-80s | %-15s | %-15s| %-15s| %-15s| %-15s\n", book.getTitle(), book.getPrice(), book.getPublisher(), book.getAuthor(), book.getSalePrice(), book.getIsbn()));
}
System.out.println(sb);
}
}
|
cs |
๐ช์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ํ๋ฉด
๐คํ๋ก์ ํธ ํ๊ณ
์ ๋ฒ ํ๋ก์ ํธ์์ ๋์์ธ ํจํด์ ๋ํด ์ ๊ฒฝ์ฐ์ง ์๊ณ ๊ทธ๋ฅ ๊ธฐ๋ฅ๋ค๋ง ์๋ํ๊ฒ ์ฝ๋ฉํ์๋ค. ๋๋ฌด ๊ฐ๋ณ๊ฒ ์๊ฐํ๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ง๋ง ๋ค๋ฅธ ๋ถ๋ค์ ์์ ๋ถ๋ถ์ด๋ผ๋ ์ค์ ์ ๋ฌด์ฒ๋ผ ์ฝ๋ฉํ์ ๊ฑฐ ๋ณด๊ณ ๋์์ธ ํจํด์ ์ ์ฉ์ํค๊ฒ ๋ ๋ฆฌํฉํ ๋ง์ ๋ง์ด ์งํ ํ์๋ค.
๋จผ์ ๊ฐ์ธ Key๊ฐ์ด ๋ค์ด ์๋ ๋ถ๋ถ์ config.yml ํ์ผ์ ๋ฃ์ด๋์ ์ ๋ณด ์๋์ ์์ผฐ๋ค. ๊ทธ๊ณณ์์ ๊ฐ์ธ์ ์ธ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ ํด๋์ค๋ฅผ ํ๋ ๋ง๋ค์ด์ DB๋ API์์๋ ์ฌํ์ฉํ์๋ค. URL ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ถ๋ณ ์์๋ก ์ ์ธํด์ฃผ์๋ค.
์คํ๋ง์์ @Autowired ํด์ Service๋ฅผ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ฒ์ฒ๋ผ Main.java์์ ๋น์ง๋์ค ๋ก์ง์ ๋ด๋นํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๊ทธ๊ณณ์ ์ ๋ณด๋ฅผ ๋ณด๋ด์ฃผ์๋ค.
API ์ฐ๋์ ์ ๋ฒ์๋ ํ์ด์ ์ฝ๊ฒ ํ์๋ค. ํ์ง๋ง ์ถ๋ ฅ ํ๋ฉด์ ํ๊ธ๊ณผ ์์ด๊ฐ ์ฐจ์งํ๋ String.format์ ๊ฐ๊ฒฉ์ด ๊ฐ๊ธฐ ๋ฌ๋ผ ๊ฐ๊ฒฉ์ด ์๋ง๋ ๋ชจ์ต์ด ๋๋ฌด ์ ๊ฒฝ์ฐ์๋ค.
java.awt ํจํค์ง๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ์์์๋ UI๋ฅผ ์ ๊ฒฝ์จ์ ๋ง๋ค ์ ์๋ค๋ ๊ฑธ ์ด๋์ ๋ณด์๋๋ฐ, ์ ๋ชฉ์์ผ ๋ณด๊ณ ์ถ์์ง๋ง, ๊ทธ๋ ๊ฒ ํ์ง ๋ชปํ์๋ค.
java.awt api๋ ์ธ์ ๊ฐ ๊ผญ ์จ๋ณด๊ณ ์ถ๋ค.
๋๊ธ