---
title: "What are the steps to connect to the database in java?"  
description: "What are the steps to connect to the database in java?"  
author: "Anonymous User"  
published: 2015-05-15  
updated: 2020-09-21  
canonical: https://www.mindstick.com/interview/2533/what-are-the-steps-to-connect-to-the-database-in-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# What are the steps to connect to the database in java?

1.Registering the driver class2.Creating connection3.Creating statement4.Executing queries5.Closing connection\
Example:

```
import java.sql.*;  class OracleCon{  public static void main(String args[]){  try{  //step1 load the driver class  Class.forName("oracle.jdbc.driver.OracleDriver");    //step2 create  the connection object  Connection con=DriverManager.getConnection(  "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");    //step3 create the statement object  Statement stmt=con.createStatement();    //step4 execute query  ResultSet rs=stmt.executeQuery("select * from emp");  while(rs.next())  System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));    //step5 close the connection object  con.close();    }catch(Exception e){ System.out.println(e);}    }  }  
```

\
\

## Answers

### Answer by Anonymous User

1.Registering the driver class2.Creating connection3.Creating statement4.Executing queries5.Closing connection\
Example:

```
import java.sql.*;  class OracleCon{  public static void main(String args[]){  try{  //step1 load the driver class  Class.forName("oracle.jdbc.driver.OracleDriver");    //step2 create  the connection object  Connection con=DriverManager.getConnection(  "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");    //step3 create the statement object  Statement stmt=con.createStatement();    //step4 execute query  ResultSet rs=stmt.executeQuery("select * from emp");  while(rs.next())  System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));    //step5 close the connection object  con.close();    }catch(Exception e){ System.out.println(e);}    }  }  
```

\
\


---

Original Source: https://www.mindstick.com/interview/2533/what-are-the-steps-to-connect-to-the-database-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
