Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday, 13 January 2018

Steps To Connect To Database in Java

JDBC Connection Steps

Jdbc Connection Steps in java

Here we are going to discuss some steps to connect to database in java jdbc. If we want to connect java application with any database then we have to follow 5 steps using jdbc.

Let's jdbc connection in java with database using some 5 steps.


There are 5 Steps to Java Database Connectivity

  1. Register or load the driver class.
  2. Establish the connection.
  3. Create the statement.
  4. Execute the queries.
  5. Close the connection.

Let's understand above mentioned jdbc database connection steps one-by-one.



1) Register or load the driver class

The first steps is to register or load the driver class by using forName()  method of Class class.

The forName() method is used to register or load the class dynamically in java.

Syntax of forName() :

public static void forName(String className)throws ClassNotFoundException

Example to load the OracleDriver class

Class.forName("oracle.jdbc.driver.OracleDriver");

2) Establish the connection 

In this step, we will create the connection object. By using getConnection() method of DriverManager class we can establish the connection with the database.

Syntax of getConnection() :

public static Connection getConnection(String url)throws SQLException

public static Connection getConnection(String url,String name,String password)throws SQLException

Example to establishing connection with oracle database


Connection con = DriverManager.getConnection(

"jdbc:oracle:thin:@localhost:1521:xe","system","password");


3) Create the statement


The object of statement is used to execute the queries with the database. By using createStatement() method of Connection interface we can create statement in jdbc.

Syntax of createStatement() :

public Statement createStatement()throws SQLException

Example to create the statement object


Statement stm = con.createStatement();

4) Execute the queries

The executeQuery() method of Statement interface is used to execute the queries to the database and this method return the object of ResultSet that can be used to get all the records from the database table.

Syntax of executeQuery() :

public ResultSet executeQuery(String sql)throws SQLException

Example to execute query

ResultSet rs = stm.executeQuery("select * from student");

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

5) Close the connection 

The close() method of Connection interface is used to close the connection.


Syntax of close() :

public void close()throws SQLException

Example to close() connection 

con.close();

Read More:

Java JDBC Example with Oracle Database.
Java JDBC Example with MySql Database.
Java ResultSet Interface in JDBC.

Here we saw all the steps for connection java application with database by using jdbc .

Share:

1 comment:

  1. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate