YJ의 새벽

SQL응용 JDBC (시퀀스 수정,확인) 본문

SelfStudy/Oracle (SQL)

SQL응용 JDBC (시퀀스 수정,확인)

YJDawn 2023. 1. 25. 20:13
  • JDBC에서 시퀀스 수정 , 확인하기

 

**EX ) 시퀀스 수정  :  ALTER SEQUENCE SQL문

public class TestJDBC {
	
	//ALTER SEQUENCE 수행하는 메소드 정의
	// --ALTER SEQUENCE 시퀀스명 MAXVALUE 변경할최댓값
	//외부에서 시퀀스이름값 받아오는 매개변수 선언 : String pseqName
	//연결객체 주소 받아오는 매개변수 선언  :  Connection pcon
	//반환값이 없다 . 
	public void alterSeq(String pseqName, Connection pcon) {
		//1. 실행할 SQL 문 문자열로 보관
		String sql = "ALTER SEQUENCE "+pseqName+
				     " MAXVALUE 1000";
		//2. Statement 변수 선언
		Statement s = null;
		//3. 결과값 변수 선언
		boolean res = true;
		
		try {
			//4. Statement 객체 생성
			s = pcon.createStatement();
			//5. ALTER SEQUENCE SQL문 실행
			res = s.execute(sql);
			//6. SQL문 실행하고 값 출력
			System.out.println("ALTER문 실행한 결과값: "+res);
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(s != null) s.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}
		
		
	}
public static void main(String[] args) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		TestJDBC test = new TestJDBC();
		try {
			java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("오라클 데이터베이스 연결 , 드라이버객체 생성");
			con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "user1", "1234");
			System.out.println("오라클 DBMS에 연결되었습니다");
			
			//새로정의한 alterSeq 호출 
			test.alterSeq("EMP_SEQ02",con);

		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(con !=null) con.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}

 

**EX ) 시퀀스 확인  :  SELECT SQL문

                 : SELECT 문은 ResultSet 을 선언해야함.

 

public class TestJDBC {
	
	//SELECT MAX_VALUE FROM USER_SEQUENCES WHERE SEQUENCE_NAME = 'EMP_SEQ02';
	// 오라클 과 연결할 Connection pcon
	// 시퀀스이름을 가져올 String pseqName
	public void selSeqMax(Connection pcon,String pseqName) {
		//1. 실행할 SQL 문 문자열로 보관
		String sql =
			"SELECT MAX_VALUE FROM USER_SEQUENCES WHERE SEQUENCE_NAME = ?";
		//2. PreparedStatement 인터페이스 사용하기위한 참조변수 선언
		PreparedStatement ps = null;
		//3. SELECT 문 저장할 변수 선언
		ResultSet rs = null;
		try {
			//4. PreparedStatement 객체생성
			ps = pcon.prepareStatement(sql);
			//5. SELECT 문의 ? 자리에 들어갈값 지정
			ps.setString(1, pseqName);
			//6. SELECT 문 실행
			rs=ps.executeQuery();
			//7. ResultSet이 갖고있는 컬럼값 가져오기
			if(rs.next()==true) {
				int value = rs.getInt(1);
				System.out.println("수정된 시퀀스 최대값은: "+ value);
			}		
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) rs.close();
				if(ps!=null) ps.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		TestJDBC test = new TestJDBC();
		try {
			java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("오라클 데이터베이스 연결 , 드라이버객체 생성");
			con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "user1", "1234");
			System.out.println("오라클 DBMS에 연결되었습니다");

	        //수정된 시퀀스값 확인
			test.selSeqMax(con, "EMP_SEQ02");
			
		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(con !=null) con.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}

 

Comments