Java Reference
In-Depth Information
28
29
if
(callableStatement.getInt(
1
) >=
1
)
30 System.out.println(firstName +
" "
+ lastName +
31
get
OUT
parameter
" is in the database"
);
32
else
33 System.out.println(firstName +
" "
+ lastName +
34
" is not in the database"
);
35 }
36 }
Enter student's first name: Jacob
Enter student's last name: Smith
Jacob Smith is in the database
Enter student's first name: John
Enter student's last name: Smith
John Smith is not in the database
The program loads a MySQL driver (line 6), connects to a MySQL database (lines 7-9),
and creates a callable statement for executing the function
studentFound
(lines 15-16).
The function's first parameter is the return value; its second and third parameters corre-
spond to the first and last names. Before executing the callable statement, the program sets the
first name and last name (lines 24-25) and registers the
OUT
parameter (line 26). The state-
ment is executed in line 27.
The function's return value is obtained in line 29. If the value is greater than or equal to
1
,
the student with the specified first and last name is found in the table.
32.23
✓
✓
Describe callable statements. How do you create instances of
CallableStatement
?
How do you execute a
CallableStatement
? How do you register
OUT
parameters
in a
CallableStatement
?
Check
Point
The database metadata such as database URL, username, JDBC driver name can be
obtained using the
DatabaseMetaData
interface and result set metadata such as table
column count and column names can be obtained using the
ResultSetMetaData
interface.
Key
Point
database metadata
JDBC provides the
DatabaseMetaData
interface for obtaining database-wide information,
and the
ResultSetMetaData
interface for obtaining information on a specific
ResultSet
.
32.7.1 Database Metadata
The
Connection
interface establishes a connection to a database. It is within the context of a
connection that SQL statements are executed and results are returned. A connection also provides
access to database metadata information that describes the capabilities of the database, supported
SQL grammar, stored procedures, and so on. To obtain an instance of
DatabaseMetaData
for
a database, use the
getMetaData
method on a
Connection
object like this:
DatabaseMetaData dbMetaData = connection.getMetaData();
If your program connects to a local MySQL database, the program in Listing 32.5 displays the
database information statements shown in Figure 32.24.
Search WWH ::
Custom Search