본문 바로가기
JAVA + DB

JAVA + DB 실습(1-1)

by pms93 2022. 8. 6.

<< Main >>

package practice;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MemberDTO member = new MemberDTO();
		String sel, id, pw, fixPw, name, phoneNumber;
		
		while (true) {
			System.out.print("1. Insert\n2. Select\n3. Update\n4. Delete\n5. Exit\n: ");
			sel = sc.next();
			
			switch (sel) {
			case "1":
				member = new MemberDTO();
				System.out.println("<< Regist >>\n" + "아이디 : ");
				member.setId(sc.next());
				System.out.println("비밀번호 : ");
				member.setPw(sc.next());
				System.out.println("이름 : ");
				member.setName(sc.next());
				System.out.println("전화번호 : ");
				member.setPhoneNumber(sc.next());
				
				Insert insert = new Insert(member);
				insert.regist(member);
				break;
				
			case "2":
				Select select = new Select();
				System.out.print("1. 하나검색\n2. 전체검색\n: ");
				sel = sc.next();
				switch (sel) {
				case "1":
					System.out.print("아이디 : ");
					id = sc.next();
					select.someSearch(id);
					break;
					
				case "2":
					select.searchAll();
					break;
					
				default :
					System.out.println("메뉴 확인 후 다시 입력하세요...");
				}
				break;
				
			case "3":
				Update update = new Update();
				member = new MemberDTO();
				System.out.print("수정하실 아이디 입력 : ");
				member.setId(sc.next());
				System.out.println("현재 비밀번호 : ");
				member.setPw(sc.next());
				System.out.print("변경 비밀번호 : ");
				fixPw = sc.next();
				System.out.println("변경 이름 : ");
				member.setName(sc.next());
				System.out.println("변경 전화번호 : ");
				member.setPhoneNumber(sc.next());
				
				update.fixInfo(member, fixPw);
				break;
				
			case "4":
				Delete delete = new Delete();
				System.out.print("삭제하실 아이디 입력 : ");
				id = sc.next();
				System.out.print("비밀번호 입력 : ");
				pw = sc.next();
				delete.delete(id, pw);
				break;
				
			case "5":
				Exit exit = new Exit();
				System.out.println("프로그램을 종료합니다.");
				return;
				
			default :
				System.out.println("메뉴 확인 후 다시 입력하세요...");
			}
		}
		
		
		
	}
}

<< MemberDTO >>

package practice;

public class MemberDTO {
	
	private String id, pw, name, phoneNumber;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	

}

 

<< MemberDAO >>

 

package practice;

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

public class MemberDAO {
	private Connection connect;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	public MemberDAO() {
		
		try {
			Class.forName("oracle.jdbc.OracleDriver");
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버를 찾을 수 없습니다.");
			e.printStackTrace();
		}
		try {
			this.connect = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
					"oracle", "oracle");
		} catch (SQLException e) {
			System.out.println("DB연결에 실패했습니다.");
			e.printStackTrace();
		}
	}
	
	public int dataCount(String id) {
		String dataCountQuery = "SELECT count(*) FROM info WHERE id = ?";
		int count = 0;
		rs = null;
		
		try {
			ps = connect.prepareStatement(dataCountQuery);
			ps.setString(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public void regist(MemberDTO member) {
		String insertQuery = "INSERT INTO info VALUES(?, ?, ?, ?)";
		try {
			ps = connect.prepareStatement(insertQuery);
			ps.setString(1, member.getId());
			ps.setString(2, member.getPw());
			ps.setString(3, member.getName());
			ps.setString(4, member.getPhoneNumber());
			int verify = ps.executeUpdate();
			if (verify != 1)
				System.out.println("등록 과정에서 오류가 발생했습니다. 다시 시도하세요...");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public ResultSet someSearch(String id) {
		String someSearchQuery = "SELECT * FROM info WHERE id = ?";
		rs = null;
		
		try {
			ps = connect.prepareStatement(someSearchQuery);
			ps.setString(1, id);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public ResultSet searchAll() {
		String searchAllQuery = "SELECT * FROM info";
		rs = null;
		
		try {
			ps = connect.prepareStatement(searchAllQuery);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public void fixInfo(MemberDTO member) {
		String fixInfoQuery = "UPDATE info SET pw = ?, name = ?, phonenumber = ? WHERE id = ?";
		try {
			ps = connect.prepareStatement(fixInfoQuery);
			ps.setString(1, member.getPw());
			ps.setString(2, member.getName());
			ps.setString(3, member.getPhoneNumber());
			ps.setString(4, member.getId());
			int verify = ps.executeUpdate();
			if (verify != 1)
				System.out.println("수정중에 오류가 발생했습니다. 다시 시도하세요...");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void delete(String id) {
		String deleteQuery = "DELETE FROM info WHERE id = ?";
		
		try {
			ps = connect.prepareStatement(deleteQuery);
			ps.setString(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void exit() {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (connect != null)
				connect.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

 

'JAVA + DB' 카테고리의 다른 글

JAVA + DB 실습 (3)(Class.View)  (0) 2022.08.09
JAVA + DB 실습 (3)(Class.run)  (0) 2022.08.09
JAVA + DB 실습(2)  (0) 2022.08.08
JAVA + DB 실습(1-2)  (0) 2022.08.06
DB와 JAVA 연동 및 Query실행  (0) 2022.08.04