Database Reference
In-Depth Information
print
(
"Flags:
%d
"
%
(
flags
))
cursor
.
close
()
The code uses the
FieldType
class, imported as follows:
from
mysql.connector
import
FieldType
The program produces this output:
Statement: SELECT name, birth FROM profile
Number of rows: 10
Number of columns: 2
--- Column 0 (name) ---
Type: 253 (VAR_STRING)
Nullable: 0
Flags: 4097
--- Column 1 (birth) ---
Type: 10 (DATE)
Nullable: 1
Flags: 128
Java
JDBC makes result set metadata available through a
ResultSetMetaData
object, ob‐
tained by calling the
getMetaData()
method of your
ResultSet
object. The metadata
object provides access to several kinds of information. Its
getColumnCount()
method
returns the number of columns in the result set. Other types of metadata, illustrated by
the following code, provide information about individual columns and take a column
index as their argument. For JDBC, column indexes begin at 1 rather than 0, unlike our
other APIs:
String
stmt
=
"SELECT name, birth FROM profile"
;
System
.
out
.
println
(
"Statement: "
+
stmt
);
Statement
s
=
conn
.
createStatement
();
s
.
executeQuery
(
stmt
);
ResultSet
rs
=
s
.
getResultSet
();
ResultSetMetaData
md
=
rs
.
getMetaData
();
// metadata information becomes available at this point ...
int
ncols
=
md
.
getColumnCount
();
System
.
out
.
println
(
"Number of columns: "
+
ncols
);
if
(
ncols
==
0
)
System
.
out
.
println
(
"Note: statement has no result set"
);
for
(
int
i
=
1
;
i
<=
ncols
;
i
++)
// column index values are 1-based
{
System
.
out
.
println
(
"--- Column "
+
i
+
" ("
+
md
.
getColumnName
(
i
)
+
") ---"
);
System
.
out
.
println
(
"getColumnDisplaySize: "
+
md
.
getColumnDisplaySize
(
i
));
System
.
out
.
println
(
"getColumnLabel: "
+
md
.
getColumnLabel
(
i
));
System
.
out
.
println
(
"getColumnType: "
+
md
.
getColumnType
(
i
));
System
.
out
.
println
(
"getColumnTypeName: "
+
md
.
getColumnTypeName
(
i
));
System
.
out
.
println
(
"getPrecision: "
+
md
.
getPrecision
(
i
));
System
.
out
.
println
(
"getScale: "
+
md
.
getScale
(
i
));