AppMain.java: 프로그램의 메인 진입점 (사용자 입력 및 메뉴 처리) User1.java: 엔티티 클래스 (데이터베이스 테이블의 레코드 형태) SQL.java: SQL 쿼리 상수 정의 User1Dao.java: DAO (Data Access Object) 클래스 MySQL DB (studydb): 실제로 데이터를 저장하는 데이터베이스
사용자 입력을 받아 CRUD 메뉴를 처리하는 프로그램의 메인 클래스
package user1;
import java.util.List;
import java.util.Scanner;
public class AppMain {
public static void main(String[] args) {
System.out.println("-------------------");
System.out.println("User1 CRUD 실습");
System.out.println("-------------------");
Scanner sc = new Scanner(System.in);
boolean isRunning = true;
User1Dao dao = User1Dao.getInstance();
while (isRunning) {
System.out.println("0:종료, 1:입력, 2:조회, 3:검색, 4:수정, 5:삭제");
System.out.print("선택> ");
int answer = sc.nextInt();
switch (answer) {
case 0:
isRunning = false;
break;
case 1:
User1 user = inputUser(sc);
dao.insertUser1(user);
System.out.println("INSERT 완료...");
break;
case 2:
List<User1> user1List = dao.selectUser1List();
for (User1 user1 : user1List) {
System.out.println(user1);
}
break;
case 3:
String uid = findUser(sc);
User1 user1 = dao.selectUser1(uid);
if (user1 != null) {
System.out.println(user1);
} else {
System.out.println("해당 아이디를 찾을 수 없습니다.");
}
break;
case 4:
User1 modifiedUser = modifyUser(sc);
dao.updateUser1(modifiedUser);
System.out.println("해당 사용자를 수정했습니다.");
break;
case 5:
String removeUid = removeUser(sc);
dao.deleteUser1(removeUid);
System.out.println("해당 사용자를 삭제했습니다.");
break;
}
}
System.out.println("프로그램 종료...");
}
public static User1 inputUser(Scanner sc) {
sc.nextLine();
System.out.print("아이디 입력 : ");
String uid = sc.nextLine();
System.out.print("이름 입력 : ");
String name = sc.nextLine();
System.out.print("휴대폰 입력 : ");
String hp = sc.nextLine();
System.out.print("나이 입력 : ");
int age = sc.nextInt();
return new User1(uid, name, hp, age);
}
public static String findUser(Scanner sc) {
sc.nextLine();
System.out.print("검색 아이디 입력 : ");
return sc.nextLine();
}
public static User1 modifyUser(Scanner sc) {
sc.nextLine();
System.out.print("수정 아이디 입력 : ");
String uid = sc.nextLine();
System.out.print("수정 이름 입력 : ");
String name = sc.nextLine();
System.out.print("수정 휴대폰 입력 : ");
String hp = sc.nextLine();
System.out.print("수정 나이 입력 : ");
int age = sc.nextInt();
return new User1(uid, name, hp, age);
}
public static String removeUser(Scanner sc) {
sc.nextLine();
System.out.print("삭제 아이디 입력 : ");
return sc.nextLine();
}
}
테이블 구조를 그대로 Java 객체로 표현 (VO: Value Object)
package user1;
public class User1 {
private String uid;
private String name;
private String hp;
private int age;
public User1() {}
public User1(String uid, String name, String hp, int age) {
this.uid = uid;
this.name = name;
this.hp = hp;
this.age = age;
}
// Getter/Setter
public String getUid() { return uid; }
public void setUid(String uid) { this.uid = uid; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getHp() { return hp; }
public void setHp(String hp) { this.hp = hp; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override
public String toString() {
return "User1 [uid=" + uid + ", name=" + name + ", hp=" + hp + ", age=" + age + "]";
}
}
SQL문을 상수로 관리하는 전용 클래스
package user1;
public class SQL {
public static final String INSERT_USER1 = "INSERT INTO `User1` VALUES (?,?,?,?)";
public static final String SELECT_USER1_LIST = "SELECT * FROM `User1`";
public static final String SELECT_USER1 = "SELECT * FROM `User1` WHERE `uid`=?";
public static final String UPDATE_USER1 = "UPDATE `User1` SET `name`=?, `hp`=?, `age`=? WHERE `uid`=?";
public static final String DELETE_USER1 = "DELETE FROM `User1` WHERE `uid`=?";
}
데이터베이스와의 CRUD를 처리하는 객체
package user1;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class User1Dao {
private static User1Dao instance = new User1Dao();
public static User1Dao getInstance() { return instance; }
private User1Dao() {}
private final String HOST = "jdbc:mysql://localhost:3306/studydb";
private final String USER = "root";
private final String PASS = "1234";
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement psmt = null;
private ResultSet rs = null;
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(HOST, USER, PASS);
}
public void insertUser1(User1 user) {
try {
conn = getConnection();
psmt = conn.prepareStatement(SQL.INSERT_USER1);
psmt.setString(1, user.getUid());
psmt.setString(2, user.getName());
psmt.setString(3, user.getHp());
psmt.setInt(4, user.getAge());
psmt.executeUpdate();
closeAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public User1 selectUser1(String uid) {
User1 user = null;
try {
conn = getConnection();
psmt = conn.prepareStatement(SQL.SELECT_USER1);
psmt.setString(1, uid);
rs = psmt.executeQuery();
if (rs.next()) {
user = new User1(
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getInt(4)
);
}
closeAll();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
public List<User1> selectUser1List() {
List<User1> list = new ArrayList<>();
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(SQL.SELECT_USER1_LIST);
while (rs.next()) {
User1 user = new User1(
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getInt(4)
);
list.add(user);
}
closeAll();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public void updateUser1(User1 user) {
try {
conn = getConnection();
psmt = conn.prepareStatement(SQL.UPDATE_USER1);
psmt.setString(1, user.getName());
psmt.setString(2, user.getHp());
psmt.setInt(3, user.getAge());
psmt.setString(4, user.getUid());
psmt.executeUpdate();
closeAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteUser1(String uid) {
try {
conn = getConnection();
psmt = conn.prepareStatement(SQL.DELETE_USER1);
psmt.setString(1, uid);
psmt.executeUpdate();
closeAll();
} catch (Exception e) {
e.printStackTrace();
}
}
// 리소스 해제 메서드
public void closeAll() throws SQLException {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (psmt != null) psmt.close();
if (conn != null) conn.close();
}
}