learn jdbc tutorial

  • WatchAdv JAVA | JDBC Session - 1|| JDBC Agenda by Durga
  • WatchAdv Java | |JDBC Session - 2 ||Introduction to Adv. Java Part - 1 by Durga sir
  • WatchAdv Java | JDBC Session - 3 ||Introduction to Adv. Java Part - 2 by Durga sir
  • WatchAdv Java | JDBC Session - 4 ||JDBC in Simple Way Part- 1 by Durga
  • WatchAdv Java | JDBC Session - 5 ||JDBC in Simple Way Part- 2 by Durga
  • WatchPrivate video
  • WatchAdv JAVA ||JDBC Session - 7 || JDBC Versions by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA | JDBC Session - 9|| Diff b/w ODBC & JDBC || Evolution of JDBC Part - 2 by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA||JDBC Session - 11 ||How JAVA is Platform Independent & Database Independent by Durga Sir
  • WatchAdv Java | JDBC Session - 12 ||JDBC API Part - 1 by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA | JDBC Session - 14|| Types of Drivers : Introduction by Durga
  • WatchAdv JAVA | JDBC Session - 15|| Types of Drivers : Type - 1 by Durga sir
  • WatchAdv JAVA | JDBC Session - 16 || Types of Drivers : Type - 2 by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA | JDBC Session - 18 || Types of Drivers : Type - 4 by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA | JDBC Session - 20 || Which JDBC Driver should be used? by Durga sir
  • WatchAdv JAVA | JDBC Session - 21 || Difference between thick & thin Drivers by Durga sir
  • WatchPrivate video
  • WatchAdv JAVA | JDBC Session - 23 || Comparison Table of All JDBC Drivers by Durga sir
  • WatchPrivate video
  • WatchPrivate video
  • WatchPrivate video
  • WatchPrivate video
  • WatchAdv Java || JDBC Session - 28 ||Prepare, send & Execute SQL Query Part- 1 by Durga Sir
  • WatchAdv Java || JDBC Session - 29 ||Prepare, send & Execute SQL Query Part- 2 by Durga Sir
  • WatchAdv Java || JDBC Session - 30 || Prepare, send & Execute SQL Query Part- 3 by Durga Sir
  • WatchAdv Java || JDBC Session - 31 ||Prepare, send & Execute SQL Query Part- 4 by Durga Sir
  • WatchPrivate video
  • WatchPrivate video
  • WatchAdv Java || JDBC Session - 34 ||Working with Type -1 Driver by Durga Sir
  • WatchAdv Java || JDBC Session - 35 || Working with Type - 2 Driver by Durga sir
  • WatchPrivate video
  • WatchAdv Java || JDBC Session - 37 || Working with Type - 3 Driver by Durga Sir
  • WatchAdv Java || JDBC Session - 38 || Working with Type - 5 Driver by Durga Sir
  • WatchAdv Java || JDBC Session - 39 ||Summary of all JDBC Drivers by Durga Sir
  • WatchAdv Java || JDBC Session - 40 || How to identify the class of a method? by Durga Sir
  • WatchJDBC Session - 41 || AutoLoading of JDBC Driver class (New Feature from JDBC 4.0V)
  • WatchAdv Java || JDBC Session - 42 || JDBC App - 1: Program to Create Table by Durga Sir
  • WatchPrivate video
  • WatchPrivate video
  • WatchPrivate video
  • WatchAdv Java || JDBC Session - 46|| JDBC - App -5 & 6 : Formatting Sql Queires with Dynamic Input
  • WatchAdv Java || JDBC Session -47|| JDBC App - 7 : Program to insert Multiple Rows || by Durga sir
  • WatchPrivate video
  • WatchPrivate video
  • WatchPrivate video

what is JDBC? addition to Java Database Connectivity

JDBC Java Database Connectivity is the Java API that manages abutting to a database, issuing queries and commands, and handling influence units got from the database. released as a part of JDK 1.1 in 1997, JDBC changed into one of the crucial first add-ons developed for the Java persistence band.

JDBC become firstly conceived as a shopper-side API, enabling a Java customer to engage with a data supply. That changed with JDCB 2.0, which covered an non-compulsory package aiding server-aspect JDBC access. each new JDBC release seeing that again has featured updates to both the client-side kit java.sql and the server-aspect kit javax.sql. JDBC four.3, essentially the most existing edition as of this autograph, changed into launched as a part of Java SE 9 in September 2017.

this article gifts a top level view of JDBC, adopted by using a fingers-on addition to the usage of the JDBC API to connect a Java customer with SQLite, a lightweight relational database.

How JDBC works

developed as an alternative to the C-primarily based ODBC inaugurate Database Connectivity API, JDBC offers a programming-stage interface that handles the mechanics of Java purposes communicating with a database or RDBMS. The JDBC interface incorporates two layers:

The JDBC API helps communication amid the Java software and the JDBC supervisor.

  • The JDBC disciplinarian supports communique amid the JDBC supervisor and the database disciplinarian.
  • JDBC is the regular API that your software cipher interacts with. underneath that’s the JDBC-adjustable driver for the database you’re using.

    determine 1 is an architectural overview of JDBC in the Java persistence band.

    JavaWorld IDG

    determine 1. JDBC in the Java chain band

    the usage of JDBC to hook up with a database

    probably the most fortunate facts of programming within the Java ecosystem is that you will likely find a reliable JDBC database adapter for whatever thing database you opt for. in this tutorial we’ll spend SQLite to get to know JDBC, specifically because it’s so convenient to use.

    The accomplish for connecting to a database with JDBC are as follows:

    set up or locate the database you are looking to access.

  • consist of the JDBC library.
  • be sure the JDBC disciplinarian you want is for your classpath.
  • consume the JDBC library to attain a affiliation to the database.
  • spend the affiliation to situation SQL commands.
  • shut the connection should you’re finished.

we will go through these steps collectively.

footfall 1. down load and installation SQLite

SQLite is a extremely bunched database. It is never intended for creation utilize, however is a pretty good choice for promptly trying things out. SQLite uses a book as its purposeful database, devoid of acute any provider or apparition installations.

To get all started with this audience, go ahead and download the SQLite sample database. achieve the .db file and reserve it somewhere you won’t forget.

This book includes both a practical book-primarily based database and pattern action and statistics we can consume.

footfall 2. acceptation JDBC Into your Java application

We may do our coding in an IDE, however coding directly in a textual content editor will more desirable reveal JDBC’s simplicity. To begin, you will should have a appropriate JDK installation on your operating gadget.

bold you’ve got the Java platform developer equipment put in, we will start by creating a simple Java application. to your text editor, paste in the cipher proven in list 1. name this book WhatIsJdbc.java.

list 1. a simple Java software category WhatIsJdbc
accessible static abandoned maincord args
equipment.out.println.”good day JavaWorld”;

Now assemble the code with the aid of entering the command: javac WhatIsJdbc.java. accumulation will output the WhatIsJdbc.class file. execute this file from the command line with the name: java WhatIsJdbc.

See “What is the JDK? Introduction to the Java Developer Kit” for more about interacting with the JDK on the command line.

after you have a fundamental Java software, you could include the JDBC libraries. paste within the code from list 2 on the head of your simple Java program.

list 2. JDBC imports acceptation java.sqlnnection;
import java.sql.DriverManager;
acceptation java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.statement;

each and every of those imports gives entry to a class that enables the common Java database affiliation:

affiliation represents the affiliation to the database.

  • DriverManager obtains the connection to the database. a different choice is DataSource, used for connection pooling.
  • SQLException handles SQL errors amid the Java application and the database.
  • ResultSet and statement model the facts effect units and SQL statements.

we are going to see each of these in action almost immediately.

step 3. Add the JDBC disciplinarian to your classpath

subsequent, you will add the SQLite driver to your classpath. A JDBC driver is a class that implements the JDBC API for a specific database.

down load the SQLite driver from GitHub. be certain to get probably the most contemporary .jar file and save it someplace you are going to remember.

The next time you execute your Java software, you’ll cull that .jar file in by means of the classpath. There are a number of the way to set the classpath. list 3 suggests the way to do it the use of a command-line change.

listing three. executing SQLite driver on the Java classpath
java.exe -classpath path-to-driversqlite-jdbc-three.23.1.jar:. WhatIsJdbc

observe that now we have set the classpath to factor at the driver and the local directory; this way Java will nevertheless find our class file.

step 4. achieve a database affiliation

The classpath now has entry to the motive force. Now, alternate your fundamental Java utility book to look like the program in listing 4.

list four. the usage of the JDBC connection classification to connect with SQLite
import java.sql.affiliation;
acceptation java.sql.DriverManager;
acceptation java.sql.SQLException;
import java.sql.ResultSet;
acceptation java.sql.remark;

classification WhatIsJdbc
public static abandoned maincord args
affiliation conn = null;
are attempting
string url = “jdbc:sqlite:direction-to-dbchinookchinook.db”;
conn = DriverManager.getConnectionurl;

gadget.out.println,bought it!”;

capture SQLException e
throw new error.”issue”, e;
are attempting
if conn != absent

catch SQLException ex

compile and execute this code. assuming all goes neatly, you will get an affirming bulletin.

Now we’re ready for some SQL commands.

step 5. question the database

With the reside connection object in hand, we will do whatever effective, like querying the database. listing 5 indicates how to query SQLite the usage of the JDBC connection and observation objects.

checklist 5. Querying the database with JDBC
import java.sql.affiliation;
acceptation java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
acceptation java.sql.remark;

category WhatIsJdbc
accessible changeless abandoned mainstring args
affiliation conn = absent;
are attempting
string url = “jdbc:sqlite:route-to-db-bookchinookchinook.db”;
conn = DriverManager.getConnectionurl;

statement stmt = null;
string query = “opt for * from albums”;
are trying
stmt = conn.createStatement;
ResultSet rs = stmt.executeQueryquestion;
whereas rs.next
string identify = rs.getString.”title”;

trap SQLException e
throw new absurdity.”issue”, e;
if stmt != null stmt.close;

catch SQLException e
throw new absurdity,difficulty”, e;
are attempting
if conn != null

catch SQLException ex

In listing 5 we use our affiliation article to achieve a press release article: conn.createStatement. We then spend this article to assassinate an SQL query: stmt.executeQueryquery.

The executeQuery command allotment a ResultSet object, which we again expend to iterate over the records with whereas rs.subsequent. in this example, make sure you see the anthology titles now we have queried on as output.

note that we also bankrupt the connection, by means of a call to conn.shut.

accomplishing greater with JDBC

to this point we now have coated the basics of the usage of JDBC to connect with a database and subject SQL instructions. whereas Statementss and ResultSets work neatly for typical scenarios, you are going to likely need additional alternatives for higher or greater complicated purposes. fortunately, the JDBC library continues evolving to fulfill best database access needs.


One convenient method to increase the pliability of your code is to substitute the statement classification with PreparedStatement, as proven in checklist 6.

checklist 6. using JDBC PreparedStatements
cord prepState = “insert into albums values ?, ?;”;

PreparedStatement prepState =

prepState.setString1, “rebellion”;
prepState.setString2, “Bob Marley and the Wailers “;

int rowsAffected = preparedStatement.executeUpdate;

PreparedStatement replaces statement’s difficult-coded ethics with query marks ?. the use of PreparedStatements optimizes your code for reclaim: a PreparedStatement is aggregate only once, and can again be reused with loads of ambit. As your cipher imperfect grows, you without problems insert new ethics into the observation, instead of hacking the string article itself.

batch updates

on every occasion an application has a few updates to challenge, doing them in batches can significantly benefit efficiency. The aspect of batching is to capture the distinct updates and bring together them together, then issue them abruptly. listing 7 makes use of JDBC’s batch the way to operate a batch replace of a couple of PreparedStatements.

record 7. Batching with PreparedStatement
prepState.setString1, “rebellion”;
prepState.setString2, “Bob Marley and the Wailers”;

prepState.setString1, “Wildflowers”;
prepState.setString2, “Tom Petty and the Heartbreakers”;

int rowsAffected = preparedStatement.executeBatch;
JDBC transactions

transactions in relational databases allow for a set of updates to be wrapped in an interaction that either succeeds or fails altogether. The fundamentals of the usage of a transaction by way of JDBC are to tell the gadget to turn off auto-commit, after which manually tell the system to commit when you are completed. by means of default, auto-commit is on, which capacity on every occasion an executeUpdate or executeInsert is run, the command is dedicated.

listing eight indicates a small allotment of a JDBC transaction.

list eight. JDBC affairs
exhaust executeUpdate assorted instances

when affiliationmmit is encountered, the entire updates wrapped inner can be tried, and if any abort, they all might be formed lower back.

there are many greater facets in JDBC four.three value exploring, together with using CallableStatement for saved strategies, the usage of DataSource altar for more advantageous software efficiency especially by the use of connection pooling, and converting a JDBC ResultSet to a Java stream.

Database-certain aspects

despite the fact every JDBC-adjustable database presents the identical amount points for abutting and interacting with a database by the use of SQL, some databases do more than others. as an example, oracle DB offers influence caching, which isn’t required through the JDBC blueprint. here’s an illustration:

conn.prepareStatement “opt for *+ result_cache * * from personnel where employee_id < : 1”;

This instance is taken from the documentation for oracle’s JDBC OCI disciplinarian.


JDBC is one in every of Java’s oldest APIs, featuring an easy-to-consume answer for probably the most perennial wants of Java application building. shining simply the few JDBC calls validated listed here will get you begun using JDBC to connect with virtually any database. once you’ve bought these commands bottomward, which you can inaugurate to discover some of the greater sophisticated options which have been developed into JDBC.

while JDBC is satisfactory for easier functions, best builders will ultimately appear to the Java chain API J in an effort to increase a greater formal information access band. J requires more up-front assignment and a greater refined figuring out of the software architecture, but it surely nets you a more consistent, isolated, and smartly-described statistics access band. See the accomplice to this text, “what’s J? addition to the Java persistence API” for more about setting up the records persistence band for your Java functions.