반응형
JDBC : Scrollable ResultSet
------------------------------
Oracle JDBC 8.1.6 부터는 8.1.5에 포함되지 않았던 JDBC Specification 2.0의
기능을 포함하여 발표하였다. 새로 포함된 기능 중 Scrollable ResultSet은
그 동안 Foware Only만 제공되었던 ResultSet에 전후 이동이 가능한 기능을
제공하여 Cursor 이동의 편리함을 줄 수 있는 기능이다.
Scrollable ResultSet을 사용하면 다음과 같은 method가 사용가능하다.
1) ResultSet.next() : Cursor를 다음 Row로 이동
2) ResultSet.previous() : Cursor를 이전 Row로 이동
3) ResultSet.first() : Cursor를 ResulSet의 처음으로 이동
4) ResultSet.last() : Cursor를 ResulSet의 마지막으로 이동
5) ResultSet.isFirst() : 현재 Cursor가 첫 Row인지를 확인
6) ResultSet.isLast() : 현재 Cursor가 마지막 Row인지를 확인
7) ResultSet.getType() : ResultSet의 Type을 Return (Type은 아래 참조 (A))
8) ResultSet.getCucurrency() : Concurrency의 Type을 Return (Type은 아래 참조 (B))
9) ResultSet.getRow() : 현재 Cursor가 가리키고 있는 Row Number
ResultSet의 Type과 Concurrency의 Type은 Statement 생성시에 선언해 주며, 각각의
Type은 다음과 같다.
* ResultSet / Concurrency Type 선언
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
* ReusltSet의 Type (A)
1) TYPE_FORWARD_ONLY : scroll이 불가능한 forwad only 형
2) TYPE_SCROLL_INSENSITIVE : scroll은 가능하나, 변경된 사항은 적용되지 않음
3) TYPE_SCROLL_SENSITIVE : scroll은 가능하며, 변경된 사항이 적용됨
* CONCURRENCY의 TYPE (B)
1) CONCUR_READ_ONLY : resultset object의 변경이 불가능
2) CONCUR_UPDATABLE : resultset object의 변경이 가능
(sample)
import java.sql.*;
public class ResultSetTest
{
public static void main(String[] args) throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
"scott", "tiger");
// Create a Statement
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Set the statement fetch size to 1
stmt.setFetchSize (1);
// Query the EMP table
ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP");
// List the result set's type, concurrency type, ..., etc
showProperty (rset);
// List the query result
System.out.println ("List ENO, ENAME and SAL from the EMP table: ");
stmt.execute("update emp set sal = 1000 where empno = 2014");
while (rset.next())
{
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
System.out.println ();
// Do some changes outside the result set
doSomeChanges (conn);
// Place the cursor right before the first row
rset.beforeFirst ();
// List the employee information again
System.out.println ("List ENO, ENAME and SAL again: ");
while (rset.next())
{
// We expect to see the changes made in "doSomeChanges()"
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Cleanup
cleanup(conn);
// Close the connection
conn.close();
}
/**
* Update the EMP table.
*/
public static void doSomeChanges (Connection conn)
throws SQLException
{
System.out.println ("Update the employee salary outside the result set\n");
Statement otherStmt = conn.createStatement ();
otherStmt.execute ("update emp set sal = sal + 500");
otherStmt.execute ("commit");
otherStmt.close ();
}
/**
* Show the result set properties like type, concurrency type, fetch
* size,..., etc.
*/
public static void showProperty (ResultSet rset) throws SQLException
{
// Verify the result set type
switch (rset.getType())
{
case ResultSet.TYPE_FORWARD_ONLY:
System.out.println ("Result set type: TYPE_FORWARD_ONLY");
break;
case ResultSet.TYPE_SCROLL_INSENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_INSENSITIVE");
break;
case ResultSet.TYPE_SCROLL_SENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_SENSITIVE");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the result set concurrency
switch (rset.getConcurrency())
{
case ResultSet.CONCUR_UPDATABLE:
System.out.println ("Result set concurrency: ResultSet.CONCUR_UPDATABLE");
break;
case ResultSet.CONCUR_READ_ONLY:
System.out.println ("Result set concurrency: ResultSet.CONCUR_READ_ONLY");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the fetch size
System.out.println ("fetch size: "+rset.getFetchSize ());
System.out.println ();
}
/**
* Generic cleanup.
*/
public static void cleanup (Connection conn) throws SQLException
{
Statement stmt = conn.createStatement ();
stmt.execute ("UPDATE EMP SET SAL = SAL - 500");
stmt.execute ("COMMIT");
stmt.close ();
}
}
------------------------------
Oracle JDBC 8.1.6 부터는 8.1.5에 포함되지 않았던 JDBC Specification 2.0의
기능을 포함하여 발표하였다. 새로 포함된 기능 중 Scrollable ResultSet은
그 동안 Foware Only만 제공되었던 ResultSet에 전후 이동이 가능한 기능을
제공하여 Cursor 이동의 편리함을 줄 수 있는 기능이다.
Scrollable ResultSet을 사용하면 다음과 같은 method가 사용가능하다.
1) ResultSet.next() : Cursor를 다음 Row로 이동
2) ResultSet.previous() : Cursor를 이전 Row로 이동
3) ResultSet.first() : Cursor를 ResulSet의 처음으로 이동
4) ResultSet.last() : Cursor를 ResulSet의 마지막으로 이동
5) ResultSet.isFirst() : 현재 Cursor가 첫 Row인지를 확인
6) ResultSet.isLast() : 현재 Cursor가 마지막 Row인지를 확인
7) ResultSet.getType() : ResultSet의 Type을 Return (Type은 아래 참조 (A))
8) ResultSet.getCucurrency() : Concurrency의 Type을 Return (Type은 아래 참조 (B))
9) ResultSet.getRow() : 현재 Cursor가 가리키고 있는 Row Number
ResultSet의 Type과 Concurrency의 Type은 Statement 생성시에 선언해 주며, 각각의
Type은 다음과 같다.
* ResultSet / Concurrency Type 선언
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
* ReusltSet의 Type (A)
1) TYPE_FORWARD_ONLY : scroll이 불가능한 forwad only 형
2) TYPE_SCROLL_INSENSITIVE : scroll은 가능하나, 변경된 사항은 적용되지 않음
3) TYPE_SCROLL_SENSITIVE : scroll은 가능하며, 변경된 사항이 적용됨
* CONCURRENCY의 TYPE (B)
1) CONCUR_READ_ONLY : resultset object의 변경이 불가능
2) CONCUR_UPDATABLE : resultset object의 변경이 가능
(sample)
import java.sql.*;
public class ResultSetTest
{
public static void main(String[] args) throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
"scott", "tiger");
// Create a Statement
Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Set the statement fetch size to 1
stmt.setFetchSize (1);
// Query the EMP table
ResultSet rset = stmt.executeQuery ("select EMPNO, ENAME, SAL from EMP");
// List the result set's type, concurrency type, ..., etc
showProperty (rset);
// List the query result
System.out.println ("List ENO, ENAME and SAL from the EMP table: ");
stmt.execute("update emp set sal = 1000 where empno = 2014");
while (rset.next())
{
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
System.out.println ();
// Do some changes outside the result set
doSomeChanges (conn);
// Place the cursor right before the first row
rset.beforeFirst ();
// List the employee information again
System.out.println ("List ENO, ENAME and SAL again: ");
while (rset.next())
{
// We expect to see the changes made in "doSomeChanges()"
System.out.println (rset.getInt(1)+" "+rset.getString(2)+" "+
rset.getInt(3));
}
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Cleanup
cleanup(conn);
// Close the connection
conn.close();
}
/**
* Update the EMP table.
*/
public static void doSomeChanges (Connection conn)
throws SQLException
{
System.out.println ("Update the employee salary outside the result set\n");
Statement otherStmt = conn.createStatement ();
otherStmt.execute ("update emp set sal = sal + 500");
otherStmt.execute ("commit");
otherStmt.close ();
}
/**
* Show the result set properties like type, concurrency type, fetch
* size,..., etc.
*/
public static void showProperty (ResultSet rset) throws SQLException
{
// Verify the result set type
switch (rset.getType())
{
case ResultSet.TYPE_FORWARD_ONLY:
System.out.println ("Result set type: TYPE_FORWARD_ONLY");
break;
case ResultSet.TYPE_SCROLL_INSENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_INSENSITIVE");
break;
case ResultSet.TYPE_SCROLL_SENSITIVE:
System.out.println ("Result set type: TYPE_SCROLL_SENSITIVE");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the result set concurrency
switch (rset.getConcurrency())
{
case ResultSet.CONCUR_UPDATABLE:
System.out.println ("Result set concurrency: ResultSet.CONCUR_UPDATABLE");
break;
case ResultSet.CONCUR_READ_ONLY:
System.out.println ("Result set concurrency: ResultSet.CONCUR_READ_ONLY");
break;
default:
System.out.println ("Invalid type");
break;
}
// Verify the fetch size
System.out.println ("fetch size: "+rset.getFetchSize ());
System.out.println ();
}
/**
* Generic cleanup.
*/
public static void cleanup (Connection conn) throws SQLException
{
Statement stmt = conn.createStatement ();
stmt.execute ("UPDATE EMP SET SAL = SAL - 500");
stmt.execute ("COMMIT");
stmt.close ();
}
}
반응형