프로젝트A. 명함 관리 프로그램
BusinessCard.java
public class BusinessCard {
public BusinessCard(String name, String number, String company) {
this.name = name;
this.number = number;
this.company = company;
}
@Override
public String toString() {
return "BusinessCard{" +
"name='" + name + '\'' +
", phone='" + number + '\'' +
", companyName='" + company + '\'' +
", createDate='" + date + '\'' +
'}';
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getNumber() {return number;}
public void setNumber(String number) {this.number = number;}
public String getCompany() {return company;}
public void setCompany(String company) {this.company = company;}
public Date getDate() {return date;}
public void setDate(Date date) {this.date = date;}
String name, number, company;
Date date;
}
BusinessCardDao.java
public class BusinessCardDao {
private String url = "jdbc:mariadb://내-주소:3306/connectdb";
private String id = "connectuser";
private String passwd ="connect123!@#";
public BusinessCard getBusinessCard(String name){
BusinessCard BC = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
}catch (Exception e){
e.printStackTrace();
}
String sql = "SELECT * FROM businesscard WHERE name LIKE CONCAT('%',?,'%')";
try(Connection conn = DriverManager.getConnection(url, id, passwd);
PreparedStatement ps = conn.prepareStatement(sql)){
ps.setString(1, name);
try(ResultSet rs = ps.executeQuery()) {
if (rs.next()){
BC = new BusinessCard(rs.getString(1), rs.getString(2), rs.getString(3));
BC.setDate(rs.getDate(4));
}
}catch (Exception e){
e.printStackTrace();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return BC;
}
public void addBusinessCard(BusinessCard BC){
int executeCount = 0;
try {
Class.forName("org.mariadb.jdbc.Driver");
}catch (Exception e){
e.printStackTrace();
}
String sql = "INSERT businesscard (name, number, company, createDate) VALUES (?,?,?, NOW())";
try(Connection conn = DriverManager.getConnection(url, id, passwd);
PreparedStatement ps = conn.prepareStatement(sql)){
ps.setString(1, BC.getName());
ps.setString(2, BC.getNumber());
ps.setString(3, BC.getCompany());
executeCount = ps.executeUpdate();
System.out.println(executeCount);
} catch (SQLException troubles) {
troubles.printStackTrace();
}
}
}
main
public class CardManage {
BusinessCardDao dao = new BusinessCardDao();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
CardManage manage = new CardManage();
int idx = 0;
while(idx != 3){
manage.printMenu();
idx = in.nextInt();
switch (idx){
case 1:
manage.makeCard();
break;
case 2:
manage.printCard();
break;
case 3:
System.out.println("프로그램 종료");
break;
default:
break;
}
}
}
public void printMenu(){
System.out.println("----------------");
System.out.println("1. 명함 입력");
System.out.println("2. 명함 검색");
System.out.println("3. 종료");
System.out.println("----------------");
System.out.print("메뉴를 입력하세요 : ");
}
public void makeCard(){
Scanner in = new Scanner(System.in);
System.out.print("이름을 입력하세요 : ");
java.lang.String name = in.nextLine();
System.out.print("전화번호를 입력하세요 : ");
java.lang.String number = in.next();
System.out.print("회사 이름를 입력하세요 : ");
java.lang.String companny = in.nextLine();
dao.addBusinessCard(new BusinessCard(name, number, companny));
}
public void printCard(){
System.out.print("검색할 이름을 입력하세요. (like)검색 : ");
Scanner in = new Scanner(System.in);
java.lang.String name = in.nextLine();
BusinessCard BC = dao.getBusinessCard(name);
if(BC!= null)
System.out.println(BC);
else
System.out.println("데이터가 없습니다.");
}
}
최근댓글