quiz
quiz



MySQL code

(Mysql Database,Table 생성)

# 데이터베이스 생성
create database membership;

# 데이터베이스 지정
use membership;

# 테이블 생성
create table memberinfo(
id char(10) primary key,
pw char(10),
name char(5),
address char(30)
);




JAVA code (Main.java)

package pack_Member;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		
		while (true) {
			Scanner scanner = new Scanner(System.in);
			System.out.println("원하는 번호를 선택한 후 입력해주세요.(번호만 입력 후 엔터)");
			System.out.println("1. 로그인    2. 회원가입    3. 전체 회원목록");
			System.out.print("번호 선택 : ");
			int num = scanner.nextInt();

			if (num ==1) {
				Login.mtd_login();
				break;
				
			} else if (num == 2) {
				SignUp.mtd_signup();
				break;
			} else if (num ==3) {
				MemberList.mtd_memlist();
				break;
			} else {
				System.out.println("번호를 정확하게 입력해주세요");
			}
		}
	}
}




JAVA code (Login.java)

package pack_Member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Login {
	
	// 로그인 구현
	
	public static void mtd_login() {
		
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		
		// 접속
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			String url = "jdbc:mysql://localhost:3306/membership?";
			url += "useSSL=false&";
			url += "serverTimezone=Asia/Seoul&";
			url += "useUnicode=true&";
			url += "characterEncoding = UTF-8";
			String uid = "root";
			String upw = "1234";
			
			conn = DriverManager.getConnection(url, uid, upw);
			System.out.println("접속이 완료 되었습니다.");
			stmt = conn.createStatement();
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
		// 로그인
		String sql = "select pw from memberinfo where id = ?";
		try {
			Scanner scanner = new Scanner(System.in);
			System.out.print("아이디를 입력해주세요 : ");
			String id = scanner.next();
			System.out.print("비밀번호를 입력해주세요 : ");
			String pw = scanner.next();
			scanner.close();
			
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1,id);
			res = pstmt.executeQuery();
			
			if (res.next()) {
				if (res.getString(1).contentEquals(pw)) {
					System.out.println("[" + id + "] 님께서 로그인 하셨습니다. 프로그램을 종료합니다.");
				} else {
					System.out.println("비밀번호를 확인해주세요. 프로그램을 종료합니다.");
				} 
			} else {
				System.out.println("아이디를 확인해주세요. 프로그램을 종료합니다.");
			}
			
			
			res.close();
			pstmt.close();
			stmt.close();
			conn.close();

		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	}

}




JAVA code (SignUp.java)

package pack_Member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class SignUp {
	
	// 회원가입 구현
	
	public static void mtd_signup() {
		Connection conn = null;
		PreparedStatement Pstmt = null;
		
		
		// 접속
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			String url = "jdbc:mysql://localhost:3306/membership?";
			url += "useSSL=false&";
			url += "serverTimezone=Asia/Seoul&";
			url += "useUnicode=true&";
			url += "characterEncoding = UTF-8";
			String uid = "root";
			String upw = "1234";
			
			conn = DriverManager.getConnection(url, uid, upw);
			System.out.println("접속이 완료 되었습니다.");
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
		// 회원가입
		
		try {
			String sql = "insert into memberinfo values (?, ?, ?, ?)";
			Pstmt = conn.prepareStatement(sql);
			String id ="";
			String pw = "";
			String name = "";
			String address = "";
		
			System.out.println("[회원가입]");
			Scanner scanner = new Scanner(System.in);
			System.out.print("아이디 입력 : ");
			id = scanner.nextLine();
			System.out.print("비밀번호 입력 : ");
			pw = scanner.nextLine();
			System.out.print("이름 : ");
			name = scanner.nextLine();
			System.out.print("주소 입력(동까지만 입력함) : ");
			address = scanner.nextLine();
			scanner.close();
			
			Pstmt.setString(1, id);
			Pstmt.setString(2, pw);
			Pstmt.setString(3, name);
			Pstmt.setString(4, address);
			
			int res = Pstmt.executeUpdate();
			
			if (res == 1) {
				System.out.println("가입되었습니다.");
				System.out.println("프로그램을 재실행하여 로그인 해주세요.");
			} else {
				System.out.println("오류가 발생하여 미입력 되었습니다.");
			}
			
			Pstmt.close();
			conn.close();
			
			
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	}
}




JAVA code (MemberList.java)

package pack_Member;

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

public class MemberList {
	
	// 회원 목록 구현
	
	public static void mtd_memlist() {
		
		Connect.mtdConnect();
		
		Connection 	conn 	= null;
		Statement 	stmt 	= null;
		ResultSet 	res 	= null;
		
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			

			String uri = "jdbc:mysql://localhost:3306/membership?"; // DB_URL
			uri += "useSSL=false&"; // 인증방식설정
			uri += "serverTimezone=Asia/Seoul&"; // 서버시간설정
			uri += "useUnicode=true&"; // 유니코드사용여부설정
			uri += "characterEncoding=UTF-8";
			
			String uid = "root";
			String upw = "1234";
			
			conn = DriverManager.getConnection(uri, uid, upw);
			stmt = conn.createStatement();
			
			String sql = "select * from memberinfo order by id asc";
			res = stmt.executeQuery(sql);
			
			
			System.out.println("아이디        비밀번호        이름                주소");
			System.out.println("======================================================");
			while (res.next()) {
				String id = res.getString("id");
				String pw= res.getString("pw");
				String name = res.getString("name");
				String address = res.getString("address");
					System.out.println("  "+ id + "           " + pw + "         " + 
							name + "         " + address);
				}
		
			
			res.close();
			stmt.close();
			conn.close();
			
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
	}

}

Leave a comment