2014년 9월 1일 월요일

ResultSet 인터페이스[자바JSP/Servlet/JDBC/MiPlatform/XPlatform/교육/JSP/서블릿강좌/Spring교육잘하는곳/자바,JSP/spring/교육추천/JSP/JDBC실무교육]

============================ 
ResultSet 인터페이스와  
============================ 

ResultSet 객체는 SQL 문장의 실행 결과(테이블 형태)를 담고 있다 
ResultSet은 SQL문내의 조건들을 만족시키는 모든 열들을 포함하고, 현재 열에서 다양한 행들로 접근할 수 있는 getXXX 메쏘드 집합을 통해서 그 열들내에 있는 데이터에 접근을 제공한다.  
실행결과 테이블의 행 검색 
한 행씩 차례로 읽어 들인다. 
boolean next() 메소드를 호출하여 다음 행으로 넘어갈 수 있다. 
더 이상 남아 있는 행이 없으면 next()는 false를 반환한다. 


실행결과 테이블의 열 검색 메소드 
현재 검색되고 있는 행 내에서의 열은 임의의 순서로 읽어 들일 수 있다. 그러나, 각 열은 왼쪽에서 오른쪽으로 최대 한번씩 만 읽어 들이는 것이 안전하다. 
XXX getXXX(String columnName) 
열 이름 columnName의 값을 데이터베이스 자료형으로부터 자바 자료형 XXX로 변환하여 읽어 들인다. 



XXX getXXX(String columnIndex) 
열의 인덱스 columnIndex(1부터 시작)로 열의 값을 읽어 들일 수도 있으며, 더 효율적이다. 
int findColumn(String columnName) 
이름 columnName을 갖는 열의 인덱스 반환, 여러 번 참조하는 열은 인덱스로 바꾸어서 검색하면 효율적이다. 



getXXX 메소드를 통하여 SQL 자료형으로부터 몇몇 자바 자료형으로 변환하여 읽어 들일 수 있으나, 다음과 같이 가장 대응되는 자바 자료형으로 읽어 들이는 것이 바람직하다. 

- ResultSet에서 Data등을 받는 메소드 

  getArray(int i)  
          Returns the value of the designated column in the current row of this ResultSet object as an Array object in the Java programming language.  
 Array getArray(String colName)  
          Returns the value of the designated column in the current row of this ResultSet object as an Array object in the Java programming language.  
 InputStream getAsciiStream(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a stream of ASCII characters.  
 InputStream getAsciiStream(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a stream of ASCII characters.  
 BigDecimal getBigDecimal(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.  
 BigDecimal getBigDecimal(int columnIndex, int scale)  
          Deprecated.    
 BigDecimal getBigDecimal(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.  
 BigDecimal getBigDecimal(String columnName, int scale)  
          Deprecated.    
 InputStream getBinaryStream(int columnIndex)  
          Gets the value of a column in the current row as a stream of Gets the value of the designated column in the current row of this ResultSet object as a binary stream of uninterpreted bytes.  
 InputStream getBinaryStream(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a stream of uninterpreted bytes.  
 Blob getBlob(int i)  
          Returns the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.  
 Blob getBlob(String colName)  
          Returns the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.  
 boolean getBoolean(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a boolean in the Java programming language.  
 boolean getBoolean(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a boolean in the Java programming language.  
 byte getByte(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a byte in the Java programming language.  
 byte getByte(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a byte in the Java programming language.  
 byte[] getBytes(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a byte array in the Java programming language.  
 byte[] getBytes(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a byte array in the Java programming language.  
 Reader getCharacterStream(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.io.Reader object.  
 Reader getCharacterStream(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.io.Reader object.  
 Clob getClob(int i)  
          Returns the value of the designated column in the current row of this ResultSet object as a Clob object in the Java programming language.  
 Clob getClob(String colName)  
          Returns the value of the designated column in the current row of this ResultSet object as a Clob object in the Java programming language.  
 int getConcurrency()  
          Returns the concurrency mode of this ResultSet object.  
 String getCursorName()  
          Gets the name of the SQL cursor used by this ResultSet object.  
 Date getDate(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.  
 Date getDate(int columnIndex, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.  
 Date getDate(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.  
 Date getDate(String columnName, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.  
 double getDouble(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a double in the Java programming language.  
 double getDouble(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a double in the Java programming language.  
 int getFetchDirection()  
          Returns the fetch direction for this ResultSet object.  
 int getFetchSize()  
          Returns the fetch size for this ResultSet object.  
 float getFloat(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.  
 float getFloat(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a float in the Java programming language.  
 int getInt(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.  
 int getInt(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.  
 long getLong(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.  
 long getLong(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.  
 ResultSetMetaData getMetaData()  
          Retrieves the number, types and properties of this ResultSet object's columns.  
 Object getObject(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.  
 Object getObject(int i, Map map)  
          Returns the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.  
 Object getObject(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.  
 Object getObject(String colName, Map map)  
          Returns the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.  
 Ref getRef(int i)  
          Returns the value of the designated column in the current row of this ResultSet object as a Ref object in the Java programming language.  
 Ref getRef(String colName)  
          Returns the value of the designated column in the current row of this ResultSet object as a Ref object in the Java programming language.  
 int getRow()  
          Retrieves the current row number.  
 short getShort(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a short in the Java programming language.  
 short getShort(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a short in the Java programming language.  
 Statement getStatement()  
          Returns the Statement object that produced this ResultSet object.  
 String getString(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.  
 String getString(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.  
 Time getTime(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.  
 Time getTime(int columnIndex, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.  
 Time getTime(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.  
 Time getTime(String columnName, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.  
 Timestamp getTimestamp(int columnIndex)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object in the Java programming language.  
 Timestamp getTimestamp(int columnIndex, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object in the Java programming language.  
 Timestamp getTimestamp(String columnName)  
          Gets the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object.  
 Timestamp getTimestamp(String columnName, Calendar cal)  
          Returns the value of the designated column in the current row of this ResultSet object as a java.sql.Timestamp object in the Java programming language.  
 int getType()  
          Returns the type of this ResultSet object.  
 InputStream getUnicodeStream(int columnIndex)  
          Deprecated. use getCharacterStream in place of getUnicodeStream  
 InputStream getUnicodeStream(String columnName)  
          Deprecated.    
 SQLWarning getWarnings()  
          Returns the first warning reported by calls on this ResultSet object.  




- ResultSet 기타메소드 
  first() Moves the cursor to the first row in this ResultSet object. 
  next() Moves the cursor down one row from its current position. 
  previous() Moves the cursor to the previous row in this ResultSet object. 
  last() Moves the cursor to the last row in this ResultSet object. 
  isLast() Indicates whether the cursor is on the last row of this ResultSet object. 



------------------------------------ 
ResultSet 메소드 예제(JDBC2.0) 
------------------------------------ 

/* Result.java JDBC2.0 API중 커서의 이동에 관련된 메소드를 실행*/ 
/*  JDBC2.0을 지원하는 JDBC Driver를 설치후 작업을 해야 합니다 */ 
import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class Result extends HttpServlet { 
  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                              throws ServletException, IOException { 
    Connection con = null; 
    Statement stmt = null; 
    ResultSet rs = null; 
    res.setContentType("text/plain;charset=euc-kr"); 
    PrintWriter out = res.getWriter();  

    try { 
      // 오라클 드라이버를 Load 한다 
      Class.forName("oracle.jdbc.driver.OracleDriver");        
      // 데이타 베이스에 접속을 한다. 
      con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ahah", "scott", "tiger")  
      // 결과 출력    
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
      rs = stmt.executeQuery("SELECT name, age FROM employees "); 
      rs.beforeFirst();  //place cursor right before 1st row 
      rs.next();        //move forward by 1 row 
      //맨처음 레코드 출력 
  String szName = rs.getString("name"); 
    int iAge = rs.getInt("age"); 
  out.println("------맨처음 레코드------"); 
      out.println(szName + "    " + iAge); 
  //맨마지막으로 이동 
      rs.last(); 
      szName = rs.getString("name"); 
      iAge = rs.getInt("age"); 
  out.println("------맨마지막 레코드------"); 
      out.println(szName + "    " + iAge); 

  //마지막이전으로 이동 
      rs.previous(); 
      szName = rs.getString("name"); 
    iAge = rs.getInt("age"); 
  out.println("------맨마지막 이전 레코드------"); 
      out.println(szName + "    " + iAge); 

  rs.last(); 
  if (rs.isLast()) { 
  out.println("------------------"); 
      out.println("마지막에 도달했슴..."); 
    out.println("------------------"); 
  } 
    } 
  

    catch(ClassNotFoundException e) {  
      out.println("Couldn't load database driver: " + e.getMessage()); 
    } 
    catch(SQLException e) {  
      out.println("SQLException caught: " + e.getMessage()); 
    } 
    finally { 
      // 언제나 데이타 베이스 연결을 종료한다. 
      try { 
        if (con != null) con.close(); 

catch (SQLException ignored) { } 
    } 
  }  





 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/jQUERY개발… 오라클자바…12-271678
 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취…오라클자바…12-111195
53 [평일주간]100%환급6건,안드로이드,자바,C#,스프링3.2,SQL,힌트/… 오라클자바…03-15957
52 [주말주간]C#, ASP.NET마스터 오라클자바…01-311115
51 [평일,기업100%환급]SQL기초에서 Schema Object까지 오라클자바…01-31874
50 [평일야간]HTML5, CSS3,Ajax, jQuery마스터과정 오라클자바…01-31808
49 [평일주간,평일야간,주말]Spring,MyBatis,Hibernate개발자과정 오라클자바…01-191093
48 [평일주간,평일야간,주말]안드로이드개발자과정 오라클자바…01-11985
47 [평일야간,주말주간]JAVA,Network&WEB&Framework 오라클자바…01-031479
46 기업100%환급/오라클/자바/스프링/안드로이드/닷넷C#/jQUERY개발… 오라클자바…12-271678
45 [평일야간,주말]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis… 오라클자바…12-191222
44 웹퍼블리싱 마스터(HTML5,CSS3,jQUERY,AJAX,JavaScript) 오라클자바…12-141216
43 [채용예정교육]오라클자바개발잘하는신입뽑기2개월과정,교육전취… 오라클자바…12-111195
42 [평일,기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 오라클자바…12-09966
41 [평일야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 오라클자바…12-011138
40 [기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍(평일주간(단기)… 오라클자바…12-011372
39 [평일야간,주말,주말야간]SQL기초에서실무까지(SQL기초,PLSQL,힌… 오라클자바…12-01856

댓글 없음:

댓글 쓰기