Open a connection to the database with the static getConnection( )
method of the JDBC DriverManager
class.
This method returns an object of the JDBC Connection
class that needs as input a user name, password, connect string that identifies the JDBC driver to use, and the name of the database to which you want to connect. The following signature takes the URL, user name, and password as separate parameters:
getConnection( String URL, String user, String password );
Where the URL is of the form:
jdbc:oracle:<drivertype>:@<database>
One example is
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@172.20.2.253:1521:aero",
"scott", "tiger" );
The above example connects user scott
with password tiger
to a database with INSTANCE_NAME
aero
through port 1521 of host 172.20.2.253
, using the Thin
driver.
The following command sets whether auto-commit is enabled or disabled on the specified connection object.
conn.setAutoCommit( false );
- By default, JDBC commits each SQL statement as it is sent to the database.
- However, for more robust error handling, you can set up a
Connection
object so it issues a series of changes that have no effect on the database until you explicitly send a commit.
- Each
Connection
is separate, and a commit on one has no effect on the statements on the other.