※ 본 포스팅은 공부한 내용 아카이빙으로 간단하게 작성하였습니다.

oracle을 통해 sql문을 처음 접했는데 가독성이 프로그래밍 언어보다는 좋았다

간단하게 insert와 update, delete에 사용했던 구문을 남겨보고자 한다.

 insert 문

public class JDBC_Insert {

  public static void main(String[] args) {
		
    Connection con = null;
    PreparedStatement pstmt = null;
		
      try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");

        Scanner sc = new Scanner(System.in);

        System.out.print("저장할 번호를 입력하세요 : ");
        int num = Integer.parseInt( sc.nextLine() );
        System.out.print("이름을 입력하세요 : ");
        String name = sc.nextLine();
        System.out.print("이메일 입력하세요 : ");
        String email = sc.nextLine();
        System.out.print("전화번호를 입력하세요 : ");
        String tel = sc.nextLine();

        String sql = "insert into customer values( ? , ? , ? , ? )";

        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, num);
        pstmt.setString(2, name);
        pstmt.setString(3, email);
        pstmt.setString(4, tel);

        int result = pstmt.executeUpdate();

        if( result == 1 ) System.out.println("레코드 추가 성공");
        else System.out.println("레코드 추가 실패");
			
		}catch(ClassNotFoundException e) {			e.printStackTrace();
		}catch(SQLException e) {			e.printStackTrace();
		}catch(Exception e) {
		}
		
		try {
			if( con != null) con.close();
			if(pstmt != null) pstmt.close();
		} catch (SQLException e) {			e.printStackTrace();
		}

	}

}

 

 update 구문

public static void main(String[] args) {
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection(url, "scott", "tiger");

        Scanner sc = new Scanner(System.in);
        System.out.println("수정할 회원의 번호를 입력하세요 : ");
        int num = Integer.parseInt(sc.nextLine());

        System.out.println("수정할 항목을 선택하세요 1. 이름 2. 이메일 3. 전화번호");
        String input = sc.nextLine();

        String sql = "";
        switch(input) {
            case "1":
                System.out.print("수정할 이름을 입력하세요 : ");
                String name = sc.nextLine();
                sql = "update customer set name=? where num=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, name);
                break;
            case "2":
                System.out.print("수정할 이메일을 입력하세요 : ");
                String email = sc.nextLine();
                sql = "update customer set email=? where num=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, email);
                break;
            case "3":
                System.out.print("수정할 전화번호를 입력하세요 : ");
                String tel = sc.nextLine();
                sql = "update customer set tel=? where num=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, tel);
                break;
        }
        pstmt.setInt(2, num);

        int result = pstmt.executeUpdate();
        if(result == 1) System.out.println("수정 성공~");
        else System.out.println("수정 실패 ㅠ");

    }catch(ClassNotFoundException e){	e.printStackTrace();
    }catch(SQLException e) {		e.printStackTrace();
    }

    try {
        if(pstmt != null) pstmt.close();
        if(con != null) con.close();
    }catch(SQLException e) { e.printStackTrace();}

}

 

 delete 구문

 

public class JDBC_Delete {

	public static void main(String[] args) {
		
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
			Scanner sc = new Scanner(System.in);

			System.out.println("삭제할 회원의 번호를 입력하세요 : ");
			int num = Integer.parseInt(sc.nextLine());
			
			String sql = "delete from customer where num=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, num);
			int result = pstmt.executeUpdate();
			if(result == 1) System.out.println("수정 성공~");
			else System.out.println("수정 실패 ㅠ");
			
		}catch(ClassNotFoundException e) {e.printStackTrace();
		}catch(SQLException e) {e.printStackTrace();
			
		}
		try {
			if(pstmt != null) pstmt.close();
			if(con != null) con.close();
		}catch(SQLException e) {e.printStackTrace();}

	}

}

 

+ Recent posts