Connecting to a Database - Java Database Connectivity

Home » Java »  Advance Java »  Java Database »  Connecting to a Database


An object of the Connection interface available in the java.sql package is used to establish a connection between a Java application and a database. You need to select a driver from the various JDBC drivers to connect to a database. You also need to specify the DSN name, user name and password while establishing connection with a database. Table 22.1 describes the various methods of the Connection interface.

Method Name
Description
changePassword() Enables you to change the password of the current user.
commint() Commits all the changes made to database since the last commit or rollback operation.
createStatement() Creates an object of the Statement interface of the java.sql package.
flushCache() Clears the object cache associated with the database connection.
rollback() Enables you to undo all the changes made since the last commit or rollback operation.
terminateStatement() Closes an object of the Statement interface and frees all the resources associated with the Statement object.
close() Closes an object of the ResultSet interface and releases all the JDBC resources that the Connection objects holds.
getAutoCommit() Retrieves the current auto-commit mode the Connection object.
getWarnings() Retrieves the first warning that is reported when the current Connection object is called.
isClosed() Returns a Boolean value that indicates whether the Connection object is closed or not.
isReadOnly() Returns a Boolean value that indicates whether the Connection object is in read-only mode or not.
prepareCall() Creates an object of the CallableStatement interface of Java.
prepareStatement() Creates an object of the PreparedStatment interface of Java.
setAutoCommit() Sets the Connection object in the auto-commit mode.
setReadOnly() Sets the Connection object in the read-only mode.
Table 22.1 Methods of the Connection Interface

To establish a connection with a database, load the JDBC-ODBC bridge driver. You need to register the new JDBC driver with the DriverManager class of the java.sql package. After the bridge driver is registered, create a database connection.

The java.lang package provides the class, Class that contains the forName() method, which is used to load the JDBC-ODBC bridge driver. After the driver is loaded, it can be used to establish a connection with the DBMS. The code of load the JDBC-ODBC bridge driver is:


In the above code, sun.jdbc.JdbcOdbcDriver is the class that contains the JDBC-ODBC bridge driver. The code returns an object associated with the class that contains the JDBC-ODBC driver.

The JdbcOdbcDriver class returns the JDBC driver that is registered with the DriverManager class of Java. The syntax to register the JDBC driver with the DriverManager class is:



The database connection is established using the getConnection() method of the DriverManager class. The getConnection() method enables the JDBC driver to connect to the database specified by the JDBC Uniform Resource Locator (URL). The JDBC URL specifies the name of the database with which you need to establish a connection. The syntax to estalish a database connection using the getConnection() method is:



In the above syntax, jdbc:odbc:DSN_name represents the JDBC URL. The following program code shows how to establish a connection with a database using DSN:

Establishing Database Connection


The above code shows how to create a connection with the DSN, MyDsn, by using the getConnection() method.




Create the Database.
Creating a DSN.
Connecting to a Database.
Creating & Executing Statements.
Closing a database connection.




JDBC - Programming:

View records from the Access/SQL table.
Inserts records into the Access/SQL table.
Updates records into the Access/SQL table.
Deletes records from the Access/SQL table.
Drops the Access/SQL table.




JDBC - Working With User Interfaces - Using Swing Compoents

Inserts records into the Access/SQL table using Swing Components
View records from the Access table/SQL using Swing JTable
Updates records into the Access/SQL table using Swing Components
Deletes records from the Access/SQL table using Swing Components





No comments:

Post a Comment