Documente Academic
Documente Profesional
Documente Cultură
Connectivity (JDBC)
Java Database Connectivity
Java Database Connectivity (JDBC)
Is an API that helps you to query a database, or
any other data source for which a JDBC driver is
available.
Using JDBC, you can directly from your Java
programs
establish a connection to a relational database
query a database for information
modify the contents of a database
The classes and interfaces in the java.sql
package
JDBC Drivers
JDBC driver
A small piece of software that instructs JDBC how to connect to
and interact with a particular type of database.
The JDBC drivers facilitate JDBC to be database independent.
They can access any database, regardless of the type, vendor,
or format of the database.
JDBC Driver Providers
Many popular databases providers have JDBC drivers that can
be used to access their databases through a Java program.
Available either as downloads or are included in the installation
package of the database.
JDBC Driver Formats
JDBC drivers come in many formats. Some are partially written
in Java and a native language. Others are written entirely in
Java. Regardless of the format, all JDBC drivers contain a Java
class through which you access the driver.
JDBC-ODBC bridge
Since Java cannot communicate directly with the ODBC API, you
The Class.forName() Method
The Class.forName()
This method is a part of JDBC
used to load a JDBC driver from within your program.
A JDBC driver needs to be loaded and registered with the
driver manager before you can use JDBC to access your
database.
JDBC drivers can register themselves, but you need to load the
driver.
This is accomplished by passing the entire class name, including
package, of the JDBC driver class into the Class.forName() method.
Example of the Class.forName() Method
To load the JDBC driver for MS Access, you would use:
Class.forName( "net.ucanaccess.jdbc.UcanaccessDriver" );
The getConnection() Method
After a JDBC driver is loaded, a connection needs to be
established with the database.
This is done via the getConnection() method of the
DriverManager class.
Three different getConnection() methods.
Method Description
getConnection(Stringurl Connects to the database specified by the
) urlparameter. This method assumes the
database does not require a username
and/or password.
getConnection(String Connects to the specified database using
url,Stringuser,String the supplied usernameand password.
password )
getConnection(String Connects to the specified database using
url,Propertiesinfo ) the list of properties specified in the
Propertiesobject. This list of properties
can contain a username and password,
along with any other database-specific
Calling the getConnection()
Method
The Database URL
jdbc:sub-protocol:data-source
jdbc, identifies this URL as a JDBC URL.
sub-protocol, identifies the sub-protocol. This
value will vary based on the driver you are
using (when using the JDBC-ODBC bridge, the
sub-protocol is always odbc)
data-source, specifies the data source to which
you wish to connect. This value will also vary
based on the driver you are using. (created
Bookstore )
jdbc:odbc:Bookstore
The Connection Object
When you invoke the getConnection() method,
the DriverManager class attempts to find an
appropriate driver to connect to the database.
When it finds one, a new Connection object is
Method Used to
executeUpdat Issue an SQL statement to the database that
e() modifies the database in some way. This method
should be used for INSERT, UPDATE, and DELETE
statements.
executeBatch Process multiple SQL statements that update the
() database.
executeQuery Query the database using a SELECTstatement.
() This method returns a ResultSetobject.
Method Description
ResultSetMetaDataVariable.getColumnName( ColumnNumberV
Procedure Reference: Query a
Database
16. Obtain the current columns value by using the result
sets getString() method.
String ColumnValueVariable =
ResultSetVariable.getString(ColumnNumberVariable );
17. Output the current columns name and value separated
by a colon.
System.out.println( ColumnNameVariable + ": " +
ColumnValue );
18. A transaction can be temporarily stored in a buffer and
can either be committed using the statement
Connectionobject.commit(); or rolled back using the
statement Connectionobject.rollback();
19. Compile, run, and observe the output of the program.
Closing ResultSet and
Statement Objects
An example for closing the result set is:
ResultSet rs;
...
// close the result set
rs.close();
An example for closing the Statement object is:
Statement stmt = conn.createStatement();
...
// close the statement object
stmt.close();
Both ResultSet and Statement objects will be closed
automatically by JDBC if you do not close them. However,
closing them yourself will assure that any system resources
used by these objects will be released immediately.