package CHAP16;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class C15_03_SelectWhereQuery {
	private static final String URL = "jdbc:mysql://localhost:3306/course";
	private static final String USER = "root";
	private static final String PASSWORD = "123456789";

	// 특정 이메일로 회원 정보 조회
	public static void getMemberByEmail(Connection connection, String email) throws SQLException {
		String query = "SELECT * FROM members WHERE email = ?";

		PreparedStatement preparedStatement = connection.prepareStatement(query);
		preparedStatement.setString(1, email);

		ResultSet resultSet = preparedStatement.executeQuery();

		System.out.println("\nMember Details:");
		if (resultSet.next()) {
			System.out.println("ID: " + resultSet.getInt("member_id") 
					+ ", Username: " + resultSet.getString("username")
					+ ", Email: " + resultSet.getString("email") 
					+ ", Phone: " + resultSet.getString("phone")
					+ ", Birthdate: " + resultSet.getDate("birthdate"));
		} 
		else {
			System.out.println("No member found with the given email: " + email);
		}
	}

	public static void main(String[] args) {
		try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
			// 조회할 이메일
			String email = "moon@hanbit.com"; // 이부분은 스캐너로 변경해볼 것.
			getMemberByEmail(connection, email);
		} catch (SQLException e) {
			System.err.println("Database connection failed: " + e.getMessage());
			e.printStackTrace();
		}
	}
}

//package CHAP16;
//
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.Scanner;
//
//public class C16_03_SelectWhereQuery {
//    private static final String URL = "jdbc:mysql://localhost:3306/course";
//    private static final String USER = "root";
//    private static final String PASSWORD = "123456789";
//
//    // 특정 이메일과 이름으로 회원 정보 조회
//    public static void getMemberByEmailAndName(Connection connection, String email, String username) throws SQLException {
//        String query = "SELECT * FROM members WHERE email = ? AND username = ?";
//
//        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
//            preparedStatement.setString(1, email);
//            preparedStatement.setString(2, username);
//
//            ResultSet resultSet = preparedStatement.executeQuery();
//
//            System.out.println("\nMember Details:");
//            if (resultSet.next()) {
//                System.out.println("ID: " + resultSet.getInt("member_id")
//                        + ", Username: " + resultSet.getString("username")
//                        + ", Email: " + resultSet.getString("email")
//                        + ", Phone: " + resultSet.getString("phone")
//                        + ", Birthdate: " + resultSet.getDate("birthdate"));
//            } else {
//                System.out.println("No member found with the given email and username.");
//            }
//        }
//    }
//
//    public static void main(String[] args) {
//        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
//             Scanner scanner = new Scanner(System.in)) {
//
//            // 사용자 입력 받기
//            System.out.print("Enter email: ");
//            String email = scanner.nextLine();
//
//            System.out.print("Enter username: ");
//            String username = scanner.nextLine();
//
//            getMemberByEmailAndName(connection, email, username);
//        } catch (SQLException e) {
//            System.err.println("Database connection failed: " + e.getMessage());
//            e.printStackTrace();
//        }
//    }
//}

