Database

[MySQL] 로그인하기 - DB & 자바 연결

웨일파도 2023. 3. 23. 16:18
반응형

이클립스 자바랑 MySQL 연결 후 로그인

package db_test;

import java.sql.*;
import java.util.*;

public class LoginMain {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		Connection conn;
		Statement stmt = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver"); // MySQL 드라이버 로드
			conn = DriverManager.getConnection
					("jdbc:mysql://localhost:3306/java", "root","test1234"); // JDBC 연결
			System.out.println("DB 연결 완료");
			
			stmt = conn.createStatement();
			
			System.out.println("=== 로그인하세요. ===");
			
			System.out.print("ID 입력 >> ");
			String inputID = scan.next();
			ResultSet srs = stmt.executeQuery
					("select * from student where id = " + inputID + ";");
			
			if (srs.next()) {
				System.out.print("비밀번호 입력 >> ");
			} else {
				System.out.println("없는 ID 입니다. 회원가입 해주세요.");
			}
			
	
            String inputPW = scan.next();
			srs = stmt.executeQuery
					("select * from student where id = " + inputID + " and pw = " + inputPW + ";");
			
		
			System.out.println("select * from student where id = " + inputID + " and pw = " + inputPW + ";");
			
			if (srs.next()) {
				System.out.println("\"로그인이 되었습니다.\"");
			} else {
				System.out.println("\"로그인이 실패했습니다.\"");
			}
			
        
		
		} catch (ClassNotFoundException e) {
			System.out.println("JDBC 드라이버 로드 에러");
		} catch (SQLException e) {
			System.out.println("DB 연결 오류");
		}
		
		
	}

	



}
반응형

'Database' 카테고리의 다른 글

[오라클DB] SQL 쿼리 연습문제  (0) 2023.03.27
[오라클DB 숙제] 3월 24일 실습문제1  (0) 2023.03.24
[오라클DB] 문자 함수  (0) 2023.03.23
[오라클DB] 연습문제3  (0) 2023.03.22
[오라클DB] 연습문제  (0) 2023.03.22