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 (Conn.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;

public class Conn {
	
	static Connection conn 			= null;
	static Statement stmt 			= null;
	static PreparedStatement pstmt 		= null;
	static ResultSet res 			= null;
	
	public Conn(Connection conn, Statement stmt, 
			PreparedStatement pstmt, ResultSet res) {
		this.conn 	= conn;
		this.stmt 	= stmt;
		this.pstmt 	= pstmt;
		this.res 	= res;
	}

	public static Connection mtd_conn() {
		
		// 접속
		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());
		}
		
		return conn;
	}

}




JAVA code (Login.java)

package pack_Member;

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

public class Login extends Conn{
	
	public Login(Connection conn, Statement stmt, 
			PreparedStatement pstmt, ResultSet res) {
		super(conn, stmt, pstmt, res);
	}
	
	public static void mtd_login() {
		
		Conn.mtd_conn();
		
		// 로그인
		try {
			String sql = "select pw from memberinfo where id = ?";
			
			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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class SignUp extends Conn{
	
	public SignUp(Connection conn, Statement stmt, 
			PreparedStatement pstmt, ResultSet res) {
		super(conn, stmt, pstmt, res);
	}
	
	public static void mtd_signup() {
		
		Conn.mtd_conn();
		
		// 회원가입
		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("오류가 발생하여 미입력 되었습니다.");
				System.out.println("다시 실행해주세요");
			}
			
			pstmt.close();
			stmt.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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MemberList extends Conn{
	


	public MemberList(Connection conn, Statement stmt, 
			PreparedStatement pstmt, ResultSet res) {
		super(conn, stmt, pstmt, res);
	}

	public static void mtd_memlist() {
		
		Conn.mtd_conn();
		
		//회원 목록 구현
		try {
			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 (SQLException e) {
			System.out.println(e.getMessage());
		}
	}

}

Leave a comment