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
์๋ ๋ฐฉ์์ ๊ด์ฌ์ด ์๋ ๊ฒฝ์ฐ ๊ณต์ ๋ฌธ์์์ ์ฃผ์ ๋ฅผ ์ฝ์ ์ ์์ต๋๋ค. ํ์ฌ ๋ด ์๋ฌด๋ ๋จ์ํ ๊ทธ๊ฒ์ด ๋ฌด์์ธ์ง ๋งํ๋ ๊ฒ์ ๋๋ค.
GO TO FULL VERSION