Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[이한솔][Week2] 웹 백엔드 과제 #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions hansol/week2/cardmanager/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.edwith.webbe</groupId>
<artifactId>cardmanager</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.edwith.webbe.cardmanager;

import org.edwith.webbe.cardmanager.dao.BusinessCardManagerDao;
import org.edwith.webbe.cardmanager.dto.BusinessCard;
import org.edwith.webbe.cardmanager.ui.CardManagerUI;

import java.util.List;

public class CardManager {
public static void main(String[] args){
CardManagerUI cardManagerUI = CardManagerUI.getInstance();
BusinessCardManagerDao businessCardManagerDao = new BusinessCardManagerDao();

while_loop:
while(true) {
cardManagerUI.printMainMenu();
int menuNumber = cardManagerUI.getMenuNumber();
switch(menuNumber){
case 1 :
BusinessCard businessCard = cardManagerUI.inputBusinessCard();
businessCardManagerDao.addBusinessCard(businessCard);
break;
case 2 :
String searchKeyword = cardManagerUI.getSearchKeyword();
List<BusinessCard> businessCardList = businessCardManagerDao.searchBusinessCard(searchKeyword);
cardManagerUI.printBusinessCards(businessCardList);
break;
case 3 :
cardManagerUI.printExitMessage();
break while_loop;
default:
cardManagerUI.printErrorMessage();
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.edwith.webbe.cardmanager.dao;

import org.edwith.webbe.cardmanager.dto.BusinessCard;

import com.mysql.cj.protocol.Resultset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessCardManagerDao {
private static String dburl = "jdbc:mysql://localhost:3306/connectdb?serverTimezone=UTC";
private static String dbUser = "connectuser";
private static String dbpassword = "connect123!@#";

public List<BusinessCard> searchBusinessCard(String keyword){
List<BusinessCard> list = new ArrayList<>();

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "SELECT name,phone,companyName FROM BusinessCard WHERE name like ?";
try (Connection conn = DriverManager.getConnection(dburl, dbUser, dbpassword);
PreparedStatement ps = conn.prepareStatement(sql)){

ps.setString(1, "%"+keyword+"%");
try(ResultSet rs = ps.executeQuery()){

while(rs.next()) {
String name = rs.getString(1);
String phone = rs.getString(2);
String companyName = rs.getString(3);

BusinessCard businessCard = new BusinessCard(name,phone,companyName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 시점에 date값이 초기화되기 떄문에 사용자에게 보여질때 생성된 시점이 아니라 호출된 시점의 시간이 보여지겠죠. 또 호출될 때마다 시간이 다르게 보일겁니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 라인 삐뚤빼뚤한거 라인포매팅으로 정리한번 해주세요~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE들에서 라인포매팅을 잘 지원해주고 있어요!

https://blog.jetbrains.com/idea/2020/06/code-formatting/

list.add(businessCard);
}
}catch (Exception e) {
e.printStackTrace();
}
}catch (Exception ex) {
ex.printStackTrace();
}
return list;
}

public BusinessCard addBusinessCard(BusinessCard businessCard){
int insertCount = 0;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} //Driver Loading
String sql = "INSERT INTO BusinessCard (name,phone,companyName) VALUES(?,?,?)";
try (Connection conn = DriverManager.getConnection(dburl, dbUser, dbpassword);
PreparedStatement ps = conn.prepareStatement(sql)){
ps.setString(1, businessCard.getName());
ps.setString(2, businessCard.getPhone());
ps.setString(3, businessCard.getCompanyName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date값이 안들어가 있네요~

DB에 date 관련된 타입의 컬럼 추가 하시고, 값한번 넣어보면서 테스트해보세요
참고: https://blog.naver.com/jihye2340/220633994462


insertCount = ps.executeUpdate();
}catch (Exception ex) {
ex.printStackTrace();
}
return businessCard;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.edwith.webbe.cardmanager.dto;

import java.util.Date;

public class BusinessCard {
private String name;
private String phone;
private String companyName;
private Date createDate;

public BusinessCard(String name, String phone, String companyName) {
this.name = name;
this.phone = phone;
this.companyName = companyName;
this.createDate = new Date();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자가 생성되는 시점에 Date값이 초기화되는 것을 인지해야합니다.

BusinessCard card = new BusinessCard() // 이 시점의 시간이 저장됨

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public Date getCreateDate() {
return createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

@Override
public String toString() {
return "BusinessCard{" +
"name='" + name + '\'' +
", phone='" + phone + '\'' +
", companyName='" + companyName + '\'' +
", createDate=" + createDate +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.edwith.webbe.cardmanager.ui;

import org.edwith.webbe.cardmanager.dto.BusinessCard;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

public class CardManagerUI {
private BufferedReader in;

private CardManagerUI(){
in = new BufferedReader(new InputStreamReader(System.in));
}

private static CardManagerUI instance = new CardManagerUI();

public static CardManagerUI getInstance(){
return instance;
}

public void printMainMenu(){
System.out.println("------------------------");
System.out.println("1. 명함 입력");
System.out.println("2. 명함 검색");
System.out.println("3. 종료");
System.out.println("------------------------");
System.out.print("메뉴를 입력하세요 : ");
}

public int getMenuNumber(){
try {
int menuNumber = Integer.parseInt(in.readLine());
return menuNumber;
}catch(Exception ex){
return -1; // 숫자로 변환 못할 경우 -1을 리턴한다.
}
}

public BusinessCard inputBusinessCard(){
try {
System.out.print("이름을 입력하세요 : ");
String name = in.readLine();
System.out.print("전화번호를 입력하세요. : ");
String phone = in.readLine();
System.out.print("회사 이름을 입력하세요. : ");
String companyName = in.readLine();
BusinessCard businessCard = new BusinessCard(name, phone, companyName);
return businessCard;
}catch(Exception ex){
System.out.println("잘못된 값을 입력했습니다. ");
return null;
}
}

public String getSearchKeyword(){
try {
System.out.print("검색할 이름을 입력하세요. (like검색) : ");
String searchKeyword = in.readLine();
return searchKeyword;
}catch(Exception ex){
System.out.println("잘못된 값을 입력했습니다. ");
return null;
}
}

public void printBusinessCards(List<BusinessCard> businessCardList){
for(BusinessCard businessCard: businessCardList){
System.out.println(businessCard);
System.out.println("---------------------------------------------------------------");
}
}

public void printExitMessage(){
System.out.println("프로그램을 종료합니다. :-) ");
}

public void printErrorMessage(){
System.out.println("잘못된 입력입니다.");
}
}