Introduction to JDBC (Java Database Connectivity)

JDBC Introduction - Video

XAMPP Installation - Video

MySQL Tutorial - Video

Establishing Foreign key using PhpMyadmin - Video

Introduction to JDBC (Java Database Connectivity)

JDBC, or Java Database Connectivity, is a Java API that provides database-independent connectivity between the Java programming language and a wide range of databases. It allows Java applications to access and manipulate data in relational databases.

JDBC is a very popular API for connecting to databases from Java applications. It is supported by all major database vendors, including Oracle, MySQL, PostgreSQL, and Microsoft SQL Server.

JDBC is a relatively simple API to use. It provides a set of classes and interfaces that allow Java applications to perform the following tasks:

  • Connect to a database
  • Execute SQL statements
  • Retrieve and process the results of SQL statements
  • Update and delete data in the database

JDBC applications can be used to develop a wide variety of database-driven applications, such as:

  • Web applications
  • Desktop applications
  • Enterprise applications

Here are some of the benefits of using JDBC in Java:

  • Database independence: JDBC applications can be used to connect to any database that supports the JDBC API. This makes Java applications more portable and easier to maintain.
  • Performance: JDBC applications can be very performant, especially when using efficient database drivers.
  • Scalability: JDBC applications can be scaled to support large databases and high volumes of traffic.

Overall, JDBC is a powerful and flexible API for connecting to databases from Java applications. It is widely supported and used by developers all over the world.

Here is an example of a simple JDBC application:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MyApp {

  public static void main(String[] args) throws SQLException {
    // Load the database driver
    Class.forName("com.mysql.cj.jdbc.Driver");

    // Connect to the database
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_database", "username", "password");

    // Create a statement
    Statement statement = connection.createStatement();

    // Execute a SQL query
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

    // Iterate over the results
    while (resultSet.next()) {
      System.out.println(resultSet.getString("name"));
    }

    // Close the resources
    statement.close();
    connection.close();
  }
}

This application will connect to the MySQL database `my_database` and execute the SQL query `SELECT * FROM users`. The results of the query will then be iterated over and printed to the console.

This is just a simple example of how to use JDBC in Java. There are many more advanced features that can be used to develop more complex database-driven applications.

Exercises

Code for establishing a connection with MySQL Database using JDBC


package practiceproject;
import java.sql.*;
public class democlass {

  public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{

    try
    {
        Connection con;
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        System.out.println("Connection successfully opened");
        con.close();
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());
    }	
  }
}