package com.heeeun.member;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;


public class DBconnection {

	String id=null;
	String paswd=null;
	String name=null;
	
	private static final String URL="jdbc:oracle:thin@168.126.146.45:1521orcl";
	private static final String DBID="leeheehun";
	private static final String DBPASSWD="990116";
	private static final String DRIVER="oracle.jdbc.driver.oracleDriver";
	
	private Connection con=null;
	private PreparedStatement pstmt = null;
	private ResultSet re = null;

	
	//데이터베이스 접속
	public DBconnection() {
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void connect() {
		try {
			con=DriverManager.getConnection(URL,DBID,DBPASSWD);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void disconnect() {
		if(pstmt != null) {
			try {
				pstmt.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		if(con != null) {
			try {
				con.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public ArrayList<MemberEntity>  getMemberList(){
		connect();
		
		ArrayList<MemberEntity> list = new ArrayList<MemberEntity>();
		
		String sql= "select * from member";
		try {
			pstmt = con.prepareStatement(sql);
			re= pstmt.executeQuery();
			
			while (re.next()) {
				MemberEntity mem = new MemberEntity();
						mem.setId(re.getString("id"));
						mem.setId(re.getString("passwd"));
						mem.setId(re.getString("name"));
						
						list.add(mem);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			disconnect();
		}
		return list;
	}
	


/*과목리스트 관한 DB불러오기*/

	public ArrayList<LectureEntity> getEnrolLecture(){
		
		connect();
		ArrayList<LectureEntity> list = new ArrayList<LectureEntity>();
		
		String sql="selet * fron enroll_Lec";
		try {
			pstmt = con.prepareStatement(sql);
			re= pstmt.executeQuery();
			
			while(re.next()) {
				LectureEntity lec= new LectureEntity();
				
				lec.setId(re.getString("id"));
				lec.setLecName(re.getString("lecName"));
				lec.setCount(re.getInt("count"));
				
				list.add(lec);
				
			}
			re.close();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			disconnect();
		}return list;
	}

/*로그인 후 과목명 선책후 수강인원에 대한 count**/

	public boolean updateEnrollLecture(String[] selectCourse) {
		connect();
		
		String sql="Update enroll_Lec set count=count+1 where lecName=?";
		try {
			pstmt = con.prepareStatement(sql);
			
			for(int i =0; i<selectCourse.length; i++) {
				pstmt.setString(1, selectCourse[i]);
				System.out.println(selectCourse[i]);
				pstmt.executeUpdate();
			}
			re.close();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			disconnect();
		}
		return true;
	}

}