5 Steps to connect to the database in java

by admin on September 3, 2014

There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:

  • Register the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

1) Register the driver class

The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.

Syntax of forName() method

  1. public static void forName(String className)throws ClassNotFoundException

Example to register the OracleDriver class

  1. Class.forName(“oracle.jdbc.driver.OracleDriver”);

2) Create the connection object

The getConnection() method of DriverManager class is used to establish connection with the database.

Syntax of getConnection() method

  1. 1public static Connection getConnection(String url)throws SQLException
  2. 2public static Connection getConnection(String url,String name,String password)
  3. throws SQLException

Example to establish connection with the Oracle database

  1. Connection con=DriverManager.getConnection(
  2. “jdbc:oracle:thin:@localhost:1521:xe”,“system”,“password”);

3) Create the Statement object

The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.

Syntax of createStatement() method

  1. public Statement createStatement()throws SQLException

Example to create the statement object

  1. Statement stmt=con.createStatement();

4) Execute the query

The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.

Syntax of executeQuery() method

  1. public ResultSet executeQuery(String sql)throws SQLException

Example to execute query

  1. ResultSet rs=stmt.executeQuery(“select * from emp”);
  2. while(rs.next()){
  3. System.out.println(rs.getInt(1)+” “+rs.getString(2));
  4. }

5) Close the connection object

By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.

Syntax of close() method

  1. public void close()throws SQLException

Example to close connection

  1. con.close();
#SQASolutionShare on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someone

Previous post:

Next post: