RowSet 작업

All lectures for KO purposes
레벨 1, 레슨 822
사용 가능

6.1 RowSet 소개

이미 알고 있듯이 JDBC 표준은 거의 20년이 되었으며 약간 구식입니다. 새로운 유형과 새로운 클래스가 천천히 추가되고 있지만 모든 곳에서 아름답게 할 수는 없습니다. 그리고 그 장소 중 하나는 ResultSet 입니다 .

데이터베이스는 더 효율적으로 만들 수 있지만 ResultSet 인터페이스 는 적합하지 않습니다. 또한 명시적으로 객체를 생성하지 않고 executeQuery().

JDBC의 제작자는 오래 생각하지 않고 이전의 모든 것과 완전히 평행한 메커니즘을 만들었습니다. 그리고 그것은 RowSet 로 알려지게 되었습니다 .

주요 이점은 다음과 같습니다.

  • RowSet은 ResultSet 인터페이스를 확장하므로 그 기능은 ResultSet보다 강력합니다.
  • RowSet은 테이블 데이터를 보다 유연하게 탐색하고 앞뒤로 스크롤할 수 있습니다.
  • RowSet은 연결이 종료된 후에도 사용할 수 있는 캐시된 데이터를 유지합니다.
  • RowSet은 새로운 연결 방법을 지원하므로 연결하지 않고도 데이터베이스에 연결할 수 있습니다. 또한 XML 데이터 원본 읽기를 지원합니다.
  • RowSet은 데이터 필터를 지원합니다.
  • RowSet은 테이블 조인 작업도 지원합니다.

행 세트 유형:

  • 캐시된 행 세트
  • FilteredRowSet
  • JdbcRowSet
  • JoinRowSet
  • WebRowSet

6.2 RowSet 객체 생성

작업 개체를 가져오는 방법에는 세 가지가 있습니다.

첫째, 전통적인 방식으로 얻은 ResultSet 의 데이터로 채울 수 있습니다 .

예를 들어 CachedRowSet을 사용하여 ResultSet 데이터를 캐시할 수 있습니다 .


Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM user");
 
RowSetFactory factory = RowSetProvider.newFactory();
CachedRowSet crs = factory.createCachedRowSet();
crs.populate(results);		// Use ResultSet to populate

둘째, 자체 데이터베이스 연결을 생성하여 완전히 독립된 RowSet 객체를 생성할 수 있습니다.


JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
rowSet.setUrl("jdbc:mysql://localhost:3306/test");
rowSet.setUsername("root");
rowSet.setPassword("secret");
 
rowSet.setCommand("SELECT * FROM user");
rowSet.execute();

셋째, 이미 존재하는 연결에 RowSet을 연결할 수 있습니다.

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
JdbcRowSet rowSet = new JdbcRowSetImpl(con);

rowSet.setCommand("SELECT * FROM user");
rowSet.execute();

6.3 RowSet 작업의 예

예 1: 캐싱 .

CachedRowSet을 사용하여 모든 데이터를 캐시하고 이미 닫힌 연결에서 읽는 코드를 작성해 보겠습니다 .

Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM user");

RowSetFactory factory = RowSetProvider.newFactory();
CachedRowSet crs = factory.createCachedRowSet();
crs.populate(results);	// Use ResultSet to populate

connection.close();		// Close the connection

// Cache data is still available
while(crs.next()) {
  	System.out.println(crs.getString(1)+"\t"+ crs.getString(2)+"\t"+ crs.getString(3));
}

예제 2: RowSet을 통해 행 변경 :

// Connect to the database
 CachedRowSet crs = rsf.createCachedRowSet();
 crs.setUrl("jdbc:mysql://localhost/test");
 crs.setUsername("root");
 crs.setPassword("root");
 crs.setCommand("SELECT * FROM user");
 crs.execute();

// This type of operation can only change standalone RowSet
// First, move the pointer to an empty (new) string, the current position is remembered
 crs.moveToInsertRow();
 crs.updateString(1, Random.nextInt());
 crs.updateString(2, "Clone" + System.currentTimeMillis());
 crs.updateString(3, "Female");
 crs.insertRow();  // Add the current (new) line to the rest of the lines
 crs.moveToCurrentRow(); // Return a pointer to the line where it was before insertion

 crs.beforeFirst();
 while(crs.next()) {
 	System.out.println(crs.getString(1) + "," + crs.getString(2) + "," + crs.getString(3));
}

// And now we can upload all our changes to the database
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/dbtest", "root", "root");
con.setAutoCommit(false); // Needed for synchronization
crs.acceptChanges(con);// Synchronize data to database

작동 방식에 관심이 있는 경우 공식 문서에서 주제를 읽을 수 있습니다. 현재 내 임무는 단순히 그것이 무엇인지 말하는 것입니다.

코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION