Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ACCESSING DATABASES WITH JDBC
#1

ACCESSING DATABASES WITH JDBC

[attachment=17406]
Using JDBC in General
In this section we present the seven standard steps for querying databases. In Section
17.2 we give two simple examples (a command-line program and a servlet) illustrating
these steps to query a Microsoft Access database.
Following is a summary; details are given in the rest of the section.
1. Load the JDBC driver. To load a driver, you specify the classname
of the database driver in the Class.forName method. By doing so,
you automatically create a driver instance and register it with the
JDBC driver manager.
2. Define the connection URL. In JDBC, a connection URL specifies
the server host, port, and database name with which to establish a
connection.
3. Establish the connection. With the connection URL, username,
and password, a network connection to the database can be established.
Once the connection is established, database queries can be
performed until the connection is closed.
4. Create a Statement object. Creating a Statement object enables
you to send queries and commands to the database.
5. Execute a query or update. Given a Statement object, you can
send SQL statements to the database by using the execute,
executeQuery, executeUpdate, or executeBatch methods.
6. Process the results. When a database query is executed, a
ResultSet is returned. The ResultSet represents a set of rows
and columns that you can process by calls to next and various
getXxx methods.
7. Close the connection. When you are finished performing queries
and processing results, you should close the connection, releasing
resources to the database.

Load the JDBC Driver
The driver is the piece of software that knows how to talk to the actual database
server. To load the driver, you just load the appropriate class; a static block in the
driver class itself automatically makes a driver instance and registers it with the
JDBC driver manager. To make your code as flexible as possible, avoid hard-coding
the reference to the classname. In Section 17.3 (Simplifying Database Access with
JDBC Utilities) we present a utility class to load drivers from a Properties file so
that the classname is not hard-coded in the program.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.