Java Database Connectivity

JDBC 

  1. Is use to connect java application with the database.
  2. JDBC has multiple APIs (building functionalities) to interact with Database.
  3. To perform Database connection from java application, you required some of the components, which are as follows
    1. Database installed on local system
    2. Required the Driver (.jar)
    3. Java Application

Java Database Connectivity

What is Database and why to use Database?

  1. Database is the application which is provided by multiple vendors.
  2. Databases has to install on machine before use.
  3. To use database, you should have understanding of SQL language.
  4. Database are used to store different kind of data.
  5. This will store data in structure format (table format) that is in row and column.
  6. There are two types of database
    1. Relation Database – Can store data in table or row and column format and also one table linked with another table.
    2. Non-relation Database – Data will not store in table format.
  7. Database are used to store high volume of data which can be further use to generate report or for analysis purpose.
  8. Also, Database will provide easy way to manage large volume of data.

 

Database vendors

  1. Mysql
  2. Oracle
  3. MySql Server
  4. H2 Database (Use as a embedded DB)
  5. IBM database
  6. Cybage Database
  7. Derby Database
  8. Postgres Database

 

To Download mysql Jar : https://mvnrepository.com/artifact/mysql/mysql-connector-java

Steps To Add Jar file into core java application

  1. Right Click on Project
  2. Build Path -> Configure Build path
  3. On Window -> got to Libraries tab
  4. Click on Classpath option and click on Add External Jars button
  5. Browse the jar file add select and click on Open
  6. Click on Apply and Close Button

 

URL:

Protocol  + HostName(DNS)   + PortNumber  + Resources

MySql: jdbc:mysql://localhost:3306/jdbc

Oracle: jdbc:oracle:thing@localhost:1521:xe

 

Steps to connect Database

  1. Register Driver

    1. This step is use to load the driver in to memory.
    2. In Register Driver you have to provide driver class name.

Mysql5: com.mysql.jdbc.Driver

Mysql8: com.mysql.cj.jdbc.Driver

Oracle: oracle.jdbc.driver.OracleDriver

  1. Example:

Class.forName(“com.mysql.cj.jdbc.Driver”);

  1. Create Connection

    1. This step is use to connect with the database
    2. To execute this step you should know URL, Username and Password of the database.
    3. You have to use Connection and DriverManager JDBC APIs which id build-in java classes and interface.
    4. Example:

Connection con = DriverManager.getConnection(url,username,password);

  1. Create Statement

    1. This step is use to hold the sql statement/query.
    2. There are 3 ways to create Statement.
      1. Statement (I)
        1. Using this you can execute the query(DML,DDL).
        2. Statement may cause SQL-injection issues.
        3. Cannot write a parameterized query.
        4. Performance wise it is slower than other 2 options.
        5. Syntax:

Statement stmt = conObj.createStatement();

  1. PreparedStatement (I)
    1. Using Prepared Statement can execute DDL, DML queries.
    2. This option is more secure than the Statement, because there are no chances of SQL injection.
    3. You can write parameterized query like below.

insert into student values(?,?,?,?)

  1. Performance wise it is faster than Statement
  2. Syntax:

PreparedStatement stmt = conObj.prepareStatement(“Query”);

 

 

 

  • CallableStatement (I)
    1. Using this type of statement, you can execute a procedure from PL/sql.
    2. You can call the procedure using this type of statement
    3. Syntax:

CallableStatement stmt = conObj.preareCall(“{call procedurename(parameter)}”);

  1. Execute Statement

    1. To execute the sql statement/query and get result back to java application.
    2. There are 3 options to execute query
      1. executeUpdate()
        1. Is use to execute all DDL and DML type of queries except Select.
        2. This method returns the int values, which is the number of row affected.
      2. executeQuery()
        1. Is use to execute DQL or Select type of query.
        2. This method return the Object of ResultSet, from which you can get the data selected by your select query.

3. execute()

  1.                                   Is use to execute all type of query.
  2.                                  This method return the Boolean value, if it returns true the you will get an int value else it will return a ResultSet
  3. Close Connection

    1. Close all the database resources.

 

Example :-

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnectionTest {
	
	public static void main(String[] args) { 
		String url = "jdbc:mysql://localhost:3306/jdbc";
		String username ="root";
		String password ="7028";
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection(url, username, password);
			System.out.println("Connection Success.....");
			con.close();
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !