Opening a Connection to a Database


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.4.9: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.4.9, using the thin driver.

The following command sets whether auto-commit is enabled or disabled on the specified connection object.
  conn.setAutoCommit( false );