Java Reference
In-Depth Information
1
// Fig. 24.29: JdbcRowSetTest.java
2
// Displaying the contents of the Authors table using JdbcRowSet.
3
import java.sql.ResultSetMetaData;
4
import java.sql.SQLException;
5
6
7
8
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
public class JdbcRowSetTest
9
{
10
// JDBC driver name and database URL
11
private static final String DATABASE_URL = "jdbc:derby:books" ;
12
private static final String USERNAME = "deitel" ;
13
private static final String PASSWORD = "deitel" ;
14
15
public static void main(String args[])
16
{
17
// connect to database books and query database
18
try (JdbcRowSet rowSet =
RowSetProvider.newFactory().createJdbcRowSet())
19
20
{
21
// specify JdbcRowSet properties
rowSet.setUrl( DATABASE_URL );
rowSet.setUsername( USERNAME );
rowSet.setPassword( PASSWORD );
rowSet.setCommand( "SELECT * FROM Authors") ; // set query
rowSet.execute(); // execute query
22
23
24
25
26
27
28
// process query results
29
ResultSetMetaData metaData = rowSet.getMetaData();
30
int numberOfColumns = metaData.getColumnCount();
31
System.out.printf( "Authors Table of Books Database:%n%n" );
32
33
// display rowset header
34
for ( int i = 1 ; i <= numberOfColumns; i++)
35
System.out.printf( "%-8s\t" , metaData.getColumnName(i));
36
System.out.println();
37
38
// display each row
39
while (
rowSet.next()
)
40
{
41
for ( int i = 1 ; i <= numberOfColumns; i++)
42
rowSet.getObject(i)
System.out.printf( "%-8s\t" ,
);
43
System.out.println();
44
}
45
}
46
catch (SQLException sqlException)
47
{
48
sqlException.printStackTrace();
49
System.exit( 1 );
50
}
51
}
52
} // end class JdbcRowSetTest
Fig. 24.29 | Displaying the contents of the Authors table using JdbcRowSet . (Part 1 of 2.)
Search WWH ::




Custom Search