Java Reference
In-Depth Information
•You use the
setEmbeddedCP.bat
or
setEmbeddedCP
file (depending on your OS platform) to con-
figure the
CLASSPATH
for working with Java DB embedded databases.
•The Java DB
ij
tool allows you to interact with Java DB from the command line. You can use
it to create databases, run SQL scripts and perform SQL queries. Each command you enter at
the
ij>
prompt must be terminated with a semicolon (
;
).
Section 24.6.1 Connecting to and Querying a Database
•Package
java.sql
contains classes and interfaces for accessing relational databases in Java.
•A
Connection
object (p. 1065) manages the connection between a Java program and a database.
Connection
objects enable programs to create SQL statements that access data.
•
DriverManager
(p. 1065) method
getConnection
(p. 1065) attempts to connect to a database at
a URL that specifies the protocol for communication, the subprotocol (p. 1065) for communi-
cation and the database name.
•
Connection
method
createStatement
(p. 1066) creates a
Statement
object (p. 1066), which can
be used to submit SQL statements to the database.
•
Statement
method
executeQuery
(p. 1066) executes a query and returns a
ResultSet
object
(p. 1066).
ResultSet
methods enable a program to manipulate query results.
•A
ResultSetMetaData
object (p. 1066) describes a
ResultSet
's contents. Programs can use meta-
data programmatically to obtain information about the
ResultSet
column names and types.
•
ResultSetMetaData
method
getColumnCount
(p. 1066) retrieves the number of
ResultSet
col-
umns.
•
ResultSet
method
next
(p. 1066) positions the
ResultSet
cursor to the next row and returns
true
if the row exists; otherwise, it returns
false
. This method must be called to begin processing
a
ResultSet
because the cursor is intially positioned before the first row.
• It's possible to extract each
ResultSet
column as a specific Java type.
ResultSetMetaData
method
getColumnType
(p. 1066) returns a
Types
(p. 1067) constant (package
java.sql
) indicating the
column's type.
•
ResultSet
get
methods typically receive as an argument either a column number (as an
int
) or a
column name (as a
String
) indicating which column's value to obtain.
•
ResultSet
row and column numbers start at 1.
•Each
Statement
object can open only one
ResultSet
at a time. When a
Statement
returns a new
ResultSet
, the
Statement
closes the prior
ResultSet
.
Section 24.6.2 Querying the
books
Database
•
TableModel
(p. 1067) method
getColumnClass
(p. 1068) returns a
Class
object that represents
the superclass of all objects in a particular column. A
JTable
(p. 1067) uses this information to
set up the default cell renderer and cell editor for that column in a
JTable
.
•
Connection
method
createStatement
has an overloaded version that receives the result type and
concurrency. The result type specifies whether the
ResultSet
's cursor is able to scroll in both di-
rections or forward only and whether the
ResultSet
is sensitive to changes. The result concur-
rency (p. 1072) specifies whether the
ResultSet
can be updated.
• Some JDBC drivers (p. 1072) do not support scrollable or updatable
ResultSet
s.
•
ResultSetMetaData
method
getColumnClassName
(p. 1073) obtains a column's fully qualified
class name.
•
TableModel
method
getColumnCount
(p. 1073) returns the number of columns in the
ResultSet
.
•
TableModel
method
getColumnName
(p. 1073) returns the column name in the
ResultSet
.