1 import java.sql.*;
2 import javax.sql.RowSet;
3 import com.sun.rowset.*;
4
5 public class RowSetPreparedStatement {
6 public static void main(String[] args)
7 throws SQLException, ClassNotFoundException {
8
9 Class.forName("com.mysql.jdbc.Driver");
10 System.out.println("Driver loaded");
11
12
13 RowSet rowSet = new JdbcRowSetImpl();
14
15
16 rowSet.setUrl("jdbc:mysql://localhost/javabook");
17 rowSet.setUsername("scott");
18 rowSet.setPassword("tiger");
19 rowSet.setCommand("select * from Student where lastName = ? " +
20 "and mi = ?");
21 rowSet.setString(1, "Smith");
22 rowSet.setString(2, "R");
23 rowSet.execute();
24
25 ResultSetMetaData rsMetaData = rowSet.getMetaData();
26 for (int i = 1; i <= rsMetaData.getColumnCount(); i++)
27 System.out.printf("%-12s\t", rsMetaData.getColumnName(i));
28 System.out.println();
29
30
31 while (rowSet.next()) {
32 for (int i = 1; i <= rsMetaData.getColumnCount(); i++)
33 System.out.printf("%-12s\t", rowSet.getObject(i));
34 System.out.println();
35 }
36
37
38 rowSet.close();
39 }
40 }