Java Reference
In-Depth Information
select name, address from userdb
select address, name from userdb
Any code that depends upon knowing the order in the database would look very strange in-
deed if the user query requested fields in a different order than they were stored in the data-
base.
Example 18-10. ResultsDecoratorHTML.java
import
import
java.io.*
java.io.*
;
import
import
java.sql.*
java.sql.*
;
import
import
com.darwinsys.util.Verbosity
com.darwinsys.util.Verbosity
;
/** Print ResultSet in HTML
*/
public
public class
class
ResultsDecoratorHTML
ResultsDecoratorHTML
extends
extends
ResultsDecorator
{
public
public
ResultsDecoratorHTML
(
PrintWriter out
,
Verbosity v
) {
super
super
(
out
,
v
);
}
public
public
int
int
write
(
ResultSet rs
)
throws
throws
IOException
,
SQLException
{
ResultSetMetaData md
=
rs
.
getMetaData
();
int
int
colCount
=
md
.
getColumnCount
();
println
(
"<table border=1>"
);
print
(
"<tr>"
);
for
for
(
int
int
i
=
1
;
i
<=
colCount
;
i
++) {
print
(
"<th>"
);
print
(
md
.
getColumnLabel
(
i
));
}
println
(
"</tr>"
);
int
int
rowCount
=
0
;
while
while
(
rs
.
next
()) {
++
rowCount
;
print
(
"<tr>"
);
for
for
(
int
int
i
=
1
;
i
<=
colCount
;
i
++) {
print
(
"<td>"
);
print
(
rs
.
getString
(
i
));
}
println
(
"</tr>"
);
}
println
(
"</table>"
);
return
return
rowCount
;