Java jdbc tutorial: Coders, Are you wondering how the Java Programming Language performs the database queries? If yes, you should definitely learn about Java JDBC Concepts. JDBC is used for database-independent connectivity between the Java programming language and a wide range of databases. Basically, it runs on a variety of platforms like Windows, Mac OS, and the different versions of UNIX.
From this BTech Geeks JDBC Core & Advanced Tutorial, you will come to learn all about JDBC like what is meant by it, Types of JDBC Drivers, Topics Covered in Core & Advanced JDBC Tutorial, Why one should use JDBC in Java, Purpose to Learn it, Applications and JDBC 4.0 Packages, Interview Questions of JDBC, etc.
This MySQL and Java JDBC Tutorial includes the following stuff:
- What is JDBC?
- Types of JDBC Drivers in Java
- List of Topics Covered in Java JDBC Tutorial
- Why Should We Use JDBC?
- Why to Learn JDBC?
- Core Components of JDBC
- Java JDBC Example
- JDBC Architecture in Java
- List of Top 10 JDBC Interview Questions
What is JDBC?
jdbc tutorial: JDBC is abbreviated as Java Database Connectivity which is a standard Java API that connects and executes the query with the database. Also, it can access any type of tabular data, notably data stored in a Relational Database. You will find JDBC in JavaSE (Java Standard Edition).
The API of JDBC utilizes the JDBC Drivers for connection with the database. JDBC API is similar to Open Database Connectivity (ODBC) provided by Microsoft.
Do Read Related Java Tutorials:
Types of JDBC Drivers in Java
A JDBC driver is a JDBC API implementation utilized for connecting to an appropriate type of database. Mainly, there are four types of JDBC drivers:
- JDBC-ODBC Bridge Driver,
- Native Driver,
- Network Protocol Driver, and
- Thin Driver
List of Topics Covered in Core & Advanced Java JDBC Tutorial
Guys, who are in dilemma to know that what topics are involved in our Java JDBC Tutorial? Can look at the below list of JDBC concepts that are from core basics to advance level. Make use of these quick links available here on JDBC and become a pro in coding JDBC Java Programs. Have a glance at them for sure before continuing with other modules of Java JDBC Tutorial:
- Java JDBC Driver
- Statement Interface in Java
- PreparedStatement in Java
- CallableStatement in Java
- Difference between Statement Vs PreparedStatement Vs CallableStatement in Java
- Java MySQL Database Connectivity with Example
- MySQL Delete Duplicate Rows but keep one
- How to create and execute Stored Procedure in MySQL?
- MySQL: Error 1264 Out of range value for a column
Why Should We Use JDBC?
Earlier, ODBC API was used as a database API to connect and execute the database queries. This ODBC API utilizes the ODBC driver that is created in C language which is unsecured and platform dependent.
That’s the reason to create a new Java API ie., JDBC API which utilizes JDBC Drivers created in java language (platform-independent and secured).
By using the Java program, we use the JDBC API to control the database and execute the following tasks:
- Connect to the database
- Perform Queries and update statements to the database
- Retrieve the result received from the database.
Why to Learn JDBC?
Learning JDBC is very important for java programmers to connect and execute database-related queries. JDBC library involves APIs for every task communicated below and they are commonly linked with database usage.
- Making a connection to a database.
- Creating SQL or MySQL statements.
- Executing SQL or MySQL queries in the database.
- Viewing & Modifying the resulting records.
Core Components of JDBC
The following are the JDBC API core components that consist of:
- JDBC Drivers
- Connections
- Statements
- ResultSets
1. JDBC Drivers: JDBC driver is a collection of classes that executes interfaces illustrated in the JDBC API for beginning database connections, communicating with the database, and closing database connections.
2. Connections: Before performing any database operation via JDBC, we have to open a database connection. For opening a database connection, we can call the getConnection() method of DriverManager class. The Syntax of Connection is as follows:
Connection connection = DriverManager.getConnection(url, user, password)
3. Statements: To execute the SQL or PL/SQL queries against the database, we use the JDBC statements. We require a statement for all individual queries. JDBC API specifies the types of statements like Statement, CallableStatement, and PreparedStatement.
4. ResultSets: A query returns the data in the form of ResultSet. To understand the query result data, ResultSet places a cursor that points to the current row in the result set.
Java JDBC Example
The following example makes you understand the basic steps to access a database using JDBC. Let’s see it clearly:
package com.vinayak.jdbc; import java.sql.*; public class JDBCDemo { public static void main( String args[]) throws SQLException, ClassNotFoundException { String driverClassName = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:XE"; String username = "scott"; String password = "tiger"; String query = "insert into stidents values(109, 'bhatt')"; // Load driver class Class.forName(driverClassName); // Obtain a connection Connection con = DriverManager.getConnection( url, username, password); // Obtain a statement Statement st = con.createStatement(); // Execute the query int count = st.executeUpdate(query); System.out.println( "number of rows affected by this query= " + count); // Closing the connection as per the // requirement with connection is completed con.close(); } } // class
JDBC Architecture in Java
The JDBC API encourages both two-tier and three-tier processing models for database access.
Fig-1: Two-tier Architecture of JDBC for Data Access
Fig-2: Three-Tier Architecture of JDBC for Data Access
List of Top 10 JDBC Interview Questions
This JDBC Tutorial for beginners and professionals also covers a few of the interview questions related to Java JDBC Core and Advanced concepts. The list of JDBC Interview Questions are as follows:
- What are the main steps in java to make JDBC connectivity?
- What is JDBC Statement?
- What is JDBC PreparedStatement?
- What is JDBC CallableStatement?
- What is the difference between execute, executequery, executeupdate?
- What is the difference between Statement and PreparedStatement in JDBC?
- How can we execute stored procedures and functions?
- Explain JDBC Savepoint.
- Explain the JDBC DatabaseMetaData interface.
- Explain JDBC ResultSetMetaData interface.