핑핑핑크젤리

JDBC 본문

💖빅데이터 과정💖

JDBC

핑핑핑크젤리 2022. 7. 11. 10:19
반응형

* 오라클 시퀀스 : 자동으로 1씩


Main에 다 때려박음

package com.ST;

import java.net.ConnectException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		Connection con = null;
		PreparedStatement pst = null;
		PreparedStatement pst2 = null;
		ResultSet rs=null;

		String name = null;
		String phone = null;
		String email = null;
		int age = 0;

		int stnum = 0;
		String name2 = null;

		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String user = "madang";
		String pass = "madang";

		System.out.println("==========학생관리 프로그램 ==========\r\n");

		while (true) {
			System.out.println("1.학생추가 2.전체명단조회 3.특정학생조회 4.학생정보수정 5.학생삭제 6.프로그램종료");
			System.out.print("메뉴선택>>");
			int memu = sc.nextInt();

			switch (memu) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			case 1:
				System.out.println("1번 선택");

				System.out.println("등록할 학생의 정보를 입력하시오.");
				System.out.print("이름 : ");
				name = sc.next();
				System.out.print("나이 : ");
				age = sc.nextInt();
				System.out.print("전화번호 : ");
				phone = sc.next();
				System.out.print("이메일 : ");
				email = sc.next();

				try {
					// 1. JDBC로딩
					Class.forName("oracle.jdbc.driver.OracleDriver");

					// 2. DB연결
					con = DriverManager.getConnection(url, user, pass);

					// 3. SQL작성
					String sql = "insert into student values(stSeq.nextval,?, ?, ?, ?)";// ? : 바인드 변수로 들어감

					// 4. 바인드 변수 채우기 / 여기 인덱스는 1부터 시작함
					pst = con.prepareStatement(sql);
					pst.setString(1, name);
					pst.setInt(2, age);
					pst.setString(3, phone);
					pst.setString(4, email);

					// 4. SQL 실행처리
					// excuteUpdate => insert, update, delete
					int cnt = pst.executeUpdate();
					if (cnt > 0) {
						System.out.println("학생추가 성공");
					} else {
						System.out.println("학생추가 실패");
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 아래부터 닫아준다.
					try {
						pst.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}

				break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			case 2:
				System.out.println("2번 선택");
				try {
					// 1. JDBC로딩
					Class.forName("oracle.jdbc.driver.OracleDriver");

					// 2. DB연결
					con = DriverManager.getConnection(url, user, pass);

					// 3. SQL작성
					String sql = "select * from student";// ? : 바인드 변수로 들어감

					pst = con.prepareStatement(sql);

					// 4. SQL 실행처리
					// executeQuery => select
					rs = pst.executeQuery();
					while (rs.next()) {
						stnum = rs.getInt("stnum");
						name = rs.getString("name");
						age = rs.getInt("age");
						phone = rs.getString("phone");
						email = rs.getString("email");
						System.out.println("------학생------");
						System.out.println("학생넘버 : " + stnum);
						System.out.println("이름 : " + name);
						System.out.println("나이 : " + age);
						System.out.println("전화번호 : " + phone);
						System.out.println("이메일 : " + email);
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 아래부터 닫아준다.
					try {
						pst.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}

				break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			case 3:
				System.out.println("3번 선택");
				System.out.println("검색할 이름 : ");
				name2 = sc.next();
				try {
					// 1. JDBC로딩
					Class.forName("oracle.jdbc.driver.OracleDriver");

					// 2. DB연결
					con = DriverManager.getConnection(url, user, pass);

					// 3. SQL작성
					String sql = "select * from student where name=?";// ? : 바인드 변수로 들어감

					pst = con.prepareStatement(sql);

					pst.setString(1, name2);

					// 4. SQL 실행처리
					// executeQuery => select
					rs = pst.executeQuery();
					while (rs.next()) {
						stnum = rs.getInt("stnum");
						name = rs.getString("name");
						age = rs.getInt("age");
						phone = rs.getString("phone");
						email = rs.getString("email");
						System.out.println("------학생------");
						System.out.println("학생넘버 : " + stnum);
						System.out.println("이름 : " + name);
						System.out.println("나이 : " + age);
						System.out.println("전화번호 : " + phone);
						System.out.println("이메일 : " + email);
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 아래부터 닫아준다.
					try {
						pst.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			case 4:
				System.out.println("4번 선택");

				System.out.println("변경할 학생 이름: ");
				name2 = sc.next();
				System.out.println("[1]이름 [2]나이 [3]전화번호 [4]이메일 [5]전체 :");
				int sel = sc.nextInt();

				if (sel == 1) {
					System.out.println("변경할 이름 : ");
					name = sc.next();
				} else if (sel == 2) {
					System.out.println("변경할 나이 : ");
					age = sc.nextInt();
				} else if (sel == 3) {
					System.out.println("변경할 전화번호 : ");
					phone = sc.next();
				} else if (sel == 4) {
					System.out.println("변경할 이메일 : ");
					email = sc.next();
				} else {
					System.out.println("변경할 이름 : ");
					name = sc.next();
					System.out.println("변경할 나이 : ");
					age = sc.nextInt();
					System.out.println("변경할 전화번호 : ");
					phone = sc.next();
					System.out.println("변경할 이메일 : ");
					email = sc.next();
				}

				try {
					// 1. JDBC로딩
					Class.forName("oracle.jdbc.driver.OracleDriver");

					// 2. DB연결
					con = DriverManager.getConnection(url, user, pass);

					// 3. SQL작성
					if (sel == 1) {
						String sql = "update student set name =? where name =?";// ? : 바인드 변수로 들어감
						pst = con.prepareStatement(sql);
						pst.setString(1, name);
						pst.setString(2, name2);
					} else if (sel == 2) {
						String sql = "update student set age =? where name =?";// ? : 바인드 변수로 들어감
						pst = con.prepareStatement(sql);
						pst.setInt(1, age);
						pst.setString(2, name2);
					} else if (sel == 3) {

						String sql = "update student set phone =? where name =?";// ? : 바인드 변수로 들어감
						pst = con.prepareStatement(sql);
						pst.setString(1, phone);
						pst.setString(2, name2);
					} else if (sel == 4) {
						String sql = "update student set email =? where name =?";// ? : 바인드 변수로 들어감
						pst = con.prepareStatement(sql);
						pst.setString(1, email);
						pst.setString(2, name2);
					} else {
						String sql = "update student set name =? , age =?, phone =?, email=?where name =?";// ? : 바인드
																											// 변수로 들어감
						pst = con.prepareStatement(sql);
						pst.setString(1, name);
						pst.setInt(2, age);
						pst.setString(3, phone);
						pst.setString(4, email);

						pst.setString(5, name2);
					}

					// 4. SQL 실행처리
					// excuteUpdate => insert, update, delete
					int cnt = pst.executeUpdate();

					if (cnt > 0) {
						System.out.println("학생수정 성공");
					} else {
						System.out.println("학생수정 실패");
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 아래부터 닫아준다.
					try {
						pst.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

			case 5:
				System.out.println("5번 선택");
				System.out.print("삭제할 학생의 이름을 입력하시오>> ");
				name2 = sc.next();
				
				try {
					// 1. JDBC로딩
					Class.forName("oracle.jdbc.driver.OracleDriver");

					// 2. DB연결
					con = DriverManager.getConnection(url, user, pass);

					// 3. SQL작성
					String sql = "delete from student where name=?";// ? : 바인드 변수로 들어감
//					String sql2 ="SELECT stSeq.CURRVAL FROM DUAL";//현재 시퀀스 값
//					String sql2 ="SELECT stSeq.CURRVAL FROM DUAL";//다음 시퀀스 값

					// 4. 바인드 변수 채우기 / 여기 인덱스는 1부터 시작함
					pst = con.prepareStatement(sql);
					pst.setString(1, name2);
					
//					rs = pst.executeQuery();
//					stnum = rs.getInt("stnum");

					
					

					// 4. SQL 실행처리
					// excuteUpdate => insert, update, delete
					int cnt = pst.executeUpdate();
					if (cnt > 0) {
						System.out.println("학생삭제 성공");
					} else {
						System.out.println("학생삭제 실패");
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 아래부터 닫아준다.
					try {
						pst.close();
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				
				
				break;
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			if (memu == 6) {
				break;
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		}

	}

}

 

반응형

'💖빅데이터 과정💖' 카테고리의 다른 글

파이썬 (2022.07.15)  (0) 2022.07.21
디자인 패턴 (어댑터)  (0) 2022.07.11
이클립스 사용자 정의 자동완성 만들기  (0) 2022.07.07
자바 (2022.07.07)  (0) 2022.07.07
자바 (2022.07.06)  (0) 2022.07.06