Steps to Connect Database in Java

Steps to connect a Java Application to Database

The following 5 steps are the basic steps involve in connecting a Java application with Database using JDBC.

  1. Register the Driver
  2. Create a Connection
  3. Create SQL Statement
  4. Execute SQL Statement
  5. Closing the connection

steps to connect to database

Register the Driver

It is first an essential part to create JDBC connection. JDBC API provides a method Class.forName() which is used to load the driver class explicitly. For example, if we want to load a jdbc-odbc driver then the we call it like following.

Example to register with JDBC-ODBC Driver

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");        

Create a Connection

After registering and loading the driver in step1, now we will create a connection using getConnection() method of DriverManager class. This method has several overloaded methods that can be used based on the requirement. Basically it require the database name, username and password to establish connection. Syntax of this method is given below.

Syntax

          getConnection(String url) getConnection(String url, String username, String password) getConnection(String url, Properties info)        

This is a sample example to establish connection with Oracle Driver

          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","username","password");        
                      import java.sql.*; class Test {  public static void main(String[] args) {   try {    //Loading driver    Class.forName("oracle.jdbc.driver.OracleDriver");     //creating connection    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");     Statement s = con.createStatement(); //creating statement     ResultSet rs = s.executeQuery("select * from Student"); //executing statement     while (rs.next()) {     System.out.println(rs.getInt(1) + " " + rs.getString(2));    }     con.close(); //closing connection   } catch (Exception e) {    e.printStacktrace();   }  } }                  

Create SQL Statement

In this step we will create statement object using createStatement() method. It is used to execute the sql queries and defined in Connection class. Syntax of the method is given below.

Syntax

          public Statement createStatement() throws SQLException        

Example to create a SQL statement

          Statement s=con.createStatement();        

Execute SQL Statement

After creating statement, now execute using executeQuery() method of Statement interface. This method is used to execute SQL statements. Syntax of the method is given below.

Syntax

          public ResultSet executeQuery(String query) throws SQLException        

Example to execute a SQL statement

In this example, we are executing a sql query to select all the records from the user table and stored into resultset that further is used to display the records.

          ResultSet rs=s.executeQuery("select * from user");   while(rs.next())   {    System.out.println(rs.getString(1)+" "+rs.getString(2));  }        

Closing the connection

This is final step which includes closing all the connection that we opened in our previous steps. After executing SQL statement you need to close the connection and release the session. The close() method of Connection interface is used to close the connection.

Syntax

          public void close() throws SQLException        

Example of closing a connection

          con.close();        

Now lets combine all these steps into a single example and create a complete example of JDBC connectivity.

Example: All Steps into one place

                      import java.sql.*; class Test {  public static void main(String[] args) {   try {    //Loading driver    Class.forName("oracle.jdbc.driver.OracleDriver");     //creating connection    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");     Statement s = con.createStatement(); //creating statement     ResultSet rs = s.executeQuery("select * from Student"); //executing statement     while (rs.next()) {     System.out.println(rs.getInt(1) + " " + rs.getString(2));    }     con.close(); //closing connection   } catch (Exception e) {    e.printStacktrace();   }  } }                  

Steps to Connect Database in Java

Source: https://www.studytonight.com/java/steps-to-connect-to-database-in-java.php

0 Response to "Steps to Connect Database in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel