본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

Update sql (ResultSet)

Resultset 을 이용한 Update sql


 

import java.sql.*;


public class UpdateSQLTest {
 public static void main(String[] args) {
  Connection con = null;
  try{
  Class.forName("oracle.jdbc.driver.OracleDriver");
  // 사용할 드라이버 로드한다.
  con = DriverManager.getConnection
    ("jdbc:oracle:thin:@127.0.0.1:1521:xe","scott","tiger");
  System.out.println("정상적으로 연결 되었습니다. ");
  
  /**
   *  키보드 입력
   *  페이지번호 5page
   *  emp 테이블의 데이터를 급여 가장 많은 것 부터 순서대로 조회
   *  5개씩 분리
   *  1-5, 6-10, 11-15 ...5개씩 분리
   *  order by 급여 역순
   *  rownum 함수  >=11and rownum<=15
   *   
   */
  String selectSql = "select * from emp";
   
  // SQL 전송 결과 검색
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery(selectSql);
  int count = 0;
  while(rs.next()==true){
   int empno = rs.getInt("empno");
   String ename = rs.getString("ename");
   String job = rs.getString("job");
   int mgr = rs.getInt("mgr");
   Date d = rs.getDate("hiredate");
   double sal = rs.getDouble("sal");
   double comm = rs.getDouble("comm");   
   int deptno = rs.getInt("deptno");
   System.out.println(
     empno+":"+ename+":"+job+":"+mgr+":"+d+":"
     +":"+sal+":"+comm+":"+deptno);
  }
  System.out.println("데이터 갯수="+count);
  con.close();
  System.out.println("정상적으로 연결해제 되었습니다.");
  }catch(ClassNotFoundException e){
   System.out.println ("jdbc 드라이버 미등록이거나 이름 오류입니다.");
  }catch(SQLException e){
   e.printStackTrace();
   System.out.println("연결정보 오류");
  }finally{
   try{
   if(!con.isClosed()) con.close();
   }catch(SQLException e){
    System.out.println("finally 오류");
   }
  }
 }
}

 

결과 개인 확인

 

 

UpdateSQLTest.java