| |

DataBase Connection using Java

There are some types to connect with Database , here in this example I’m going to work with Oracle Database.

I’m using type-4 driver to connect to the data   base.

there are some steps to follow :

Step 1:

know your database url , username ,password .

and make sure that ojdbc14.jar file should be in class path .

Step 2:

1st we need  to load the driver , driver class is loaded into memory dynamically by using the following library method

Class.forName(“oracle.jdbc.driver.OracleDriver”);  or DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

–> when the above method is executed three things happen in the background:

1) Driver class is loaded into memory

2) Driver class object is created.

3)Driver instance is registered with DriverManager.

Step 3:

in this step we have to prepare connection string , this string describes the detailed location of the Database and it’s username and password.

Step 4:

a sample program to connect to database.

package Oracle.connection;
import java.sql.*;

/**
*
* @author kattavijay
*/
public class OracleConnection {
public static void main(String a[])throws Exception
{
    System.out.println("n Testing for Database connection");
  //  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  //loading or registering driver
    Class.forName("oracle.jdbc.driver.OracleDriver"); //loading or registering the driver
  
    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","scott","tiger"); // database url with username and password
    System.out.println("connected to database");
}
}

Output :

conneted to database

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *