Database Reference
In-Depth Information
int
nrows
=
0
;
int
[]
width
=
new
int
[
ncols
+
1
];
// array to store column widths
StringBuffer
b
=
new
StringBuffer
();
// buffer to hold bar line
// calculate column widths
for
(
int
i
=
1
;
i
<=
ncols
;
i
++)
{
// some drivers return -1 for getColumnDisplaySize();
// if so, we'll override that with the column name length
width
[
i
]
=
md
.
getColumnDisplaySize
(
i
);
if
(
width
[
i
]
<
md
.
getColumnName
(
i
).
length
())
width
[
i
]
=
md
.
getColumnName
(
i
).
length
();
// isNullable() returns 1/0, not true/false
if
(
width
[
i
]
<
4
&&
md
.
isNullable
(
i
)
!=
0
)
width
[
i
]
=
4
;
}
// construct +---+---...+ line
b
.
append
(
"+"
);
for
(
int
i
=
1
;
i
<=
ncols
;
i
++)
{
for
(
int
j
=
0
;
j
<
width
[
i
];
j
++)
b
.
append
(
"-"
);
b
.
append
(
"+"
);
}
// print bar line, column headers, bar line
System
.
out
.
println
(
b
.
toString
());
System
.
out
.
print
(
"|"
);
for
(
int
i
=
1
;
i
<=
ncols
;
i
++)
{
System
.
out
.
print
(
md
.
getColumnName
(
i
));
for
(
int
j
=
md
.
getColumnName
(
i
).
length
();
j
<
width
[
i
];
j
++)
System
.
out
.
print
(
" "
);
System
.
out
.
print
(
"|"
);
}
System
.
out
.
println
();
System
.
out
.
println
(
b
.
toString
());
// print contents of result set
while
(
rs
.
next
())
{
++
nrows
;
System
.
out
.
print
(
"|"
);
for
(
int
i
=
1
;
i
<=
ncols
;
i
++)
{
String
s
=
rs
.
getString
(
i
);
if
(
rs
.
wasNull
())
s
=
"NULL"
;
System
.
out
.
print
(
s
);
for
(
int
j
=
s
.
length
();
j
<
width
[
i
];
j
++)
System
.
out
.
print
(
" "
);