JDBC

JDBC stands for Java Database Connectivity. It is a Java API to connect and execute the query with the database. JDBC uses JDBC drivers to connect with the database. It can access any kind of tabular data, especially data stored in a Relational Database.

In addition, JDBC is an advancement for ODBC which was platform dependent. To remove dependence, JDBC was developed. It consists of classes and interfaces written in Java. In other words, it acts as a channel between your Java program and databases. It establishes a link between the two.

Steps to connect to database using JDBC –
  • Loading the Driver – It means to register your driver. It can be achieved in two ways –
    • Class.forName() – Load the driver’s class file into memory at run time.
      • Class.forName(“oracle.jdbc.driver.OracleDriver”);
    • DriverManager.registerDriver() – The inbuilt class DriverManager consist of a static member register. To load the driver, just call the constructor of the driver class at compile time.
      • DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  • Creating connection – To establish a connection with the database we use the getConnection() method of DriverManager class.
    • Connection con = DriverManager.getConnection (url,user,password);
  • Creating a statement – The JDBCStatement, CallableStatement, and PreparedStatement interfaces define some methods. In addition, these methods enable you to send SQL commands and receive data from your database.
    • Statement st = con.createStatement();
  • Executing query –
    • executeQuery() – To execute queries of retrieving values from the database, we use the executeQuery method. This method returns the object of ResultSet. As a result, that can be used to get all the records of a table.
    • executeUpdate(sql query) – To execute queries of updating/inserting, we use executeUpdate method.
  • Closing connection – It will close the objects of Statement and ResultSet automatically. To close the connection, we use the close() method of Connection interface.
    • con.close();
import java.sql.*;
import java.util.*;

class JDBCdemo {
	public static void main(String args[]) {
		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String user = "user";
		String password = "test123";
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter name");
		String name = scanner.next();
		
		try {
			//Loading the driver	DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
			
			//Creating the connection
			Connection connection = DriverManager.getConnection(url, user, password);
			
			//Creating statement
			Statement statement = connection.createStatement();
			
			//Executing query
			statement.executeUpdate("insert into student values("'+name+'"));
			ResultSet resultSet = statement.executeQuery("select * from student");
			while(resultSet.next()) {
				System.out.println(resultSet.getInt(1)+" "+resultSet.getString(2));
			}
			
			//Closing the connection
			connection.close();
			
		} catch(Exception e)  {
			System.out.println(e);
		}
	}
}