Introduction

The JDBC(Java Database Connectivity) API(Collection of classes and interfaces) is a technique through which we connect our program to the database smoothly. Java is one of the best languages that provides a connection through to the database smoothly, in comparison to other languages.
Java Program ||--->JDBC Api ||--->Driver||--->||Database Source
Figure 1: Description of the working of Jdbc
Steps of Jdbc for connecting the database
  1. Loading the JDBC driver.
  2. Connection to the Dbms.
  3. Creating and executing a statement.
  4. Processing data returned by the Dbms
  5. Close the connection with the Dbms.
connection in java
We have to import the SQL package, which resides in the java .lang package for performing these steps. After this step, we have to write our code for connecting to any database.
In this way, we connect a java program using the Jdbc.
Simple program for creating connection in Java
  1. import java.sql.*;
  2. class CreateConnection
  3. {
  4. public static void main(String s[])
  5. {
  6. try
  7. {
  8. //Loading the Driver
  9. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  10. //Creating the Connection
  11. Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","System","tiger");
  12. if(con!=null)
  13. {
  14. System.out.println("Connection Successfull");
  15. }
  16. else
  17. {
  18. System.out.println("Connection could not be established");
  19. }
  20. //closing the connection
  21. conn.close();
  22. }
  23. catch(SqlException e)
  24. {
  25. System.out.println("Exception occured"+ e);
  26. }
  27. }
  28. }
After compilation and execution of above program we get the output as:
jdbc
Thank You.....