0% found this document useful (0 votes)
4 views

JDBC

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

JDBC

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Definition of JDBC(Java Database Connectivity)

JDBC is an API(Application programming interface) used in Java programming to interact with


databases. The classes and interfaces of JDBC allow the application to send requests made by
users to the specified database. The current version of JDBC is JDBC 4.3, released on 21st
September 2017.
Purpose of JDBC
Enterprise applications created using the JAVA EE technology need to interact with databases to
store application-specific information. So, interacting with a database requires efficient database
connectivity, which can be achieved by using the ODBC(Open database connectivity) driver.
This driver is used with JDBC to interact or communicate with various kinds of databases such
as Oracle, MS Access, Mysql, and SQL server database.
Components of JDBC
There are generally four main components of JDBC through which it can interact with a
database. They are as mentioned below:
1. JDBC API: It provides various methods and interfaces for easy communication with the
database. It provides two packages as follows, which contain the java SE and Java EE
platforms to exhibit WORA(write once run anywhere) capabilities. The java.sql package
contains interfaces and classes of JDBC API
2. JDBC Driver manager: It loads a database-specific driver in an application to establish
a connection with a database. It is used to make a database-specific call to the database to
process the user request.
3. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation)
being performed by JDBC Drivers.
4. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge
translates the JDBC method call to the ODBC function call. It makes use of the
sun.jdbc.odbc package which includes a native library to access ODBC characteristics
Types of JDBC Architecture(2-tier and 3-tier)
The JDBC architecture consists of two-tier and three-tier processing models to access a database.
They are as described below:
Two-tier model: A java application communicates directly to the data source. The JDBC driver
enables the communication between the application and the data source. When a user sends a
query to the data source, the answers for those queries are sent back to the user in the form of
results.
The data source can be located on a different machine on a network to which a user is connected.
This is known as a client/server configuration, where the user’s machine acts as a client, and the
machine has the data source running acts as the server.
Three-tier model: In this, the user’s queries are sent to middle-tier services, from which the
commands are again sent to the data source. The results are sent back to the middle tier, and from
there to the user.
This type of model is found very useful by management information system directors.

JDBC – SQL Syntax


Structured Query Language (SQL) is a standardized language that allows you to perform
operations on a database, such as creating entries, reading content, updating content, and deleting
entries.
SQL is supported by almost any database you will likely use, and it allows you to write database
code independently of the underlying database.
Create Database
The CREATE DATABASE statement is used for creating a new database. The syntax is –
SQL> CREATE DATABASE DATABASE_NAME;
Example
The following SQL statement creates a Database named EMP –
SQL> CREATE DATABASE EMP;
Drop Database
The DROP DATABASE statement is used for deleting an existing database. The syntax is –
SQL> DROP DATABASE DATABASE_NAME;
Note – To create or drop a database you should have administrator privilege on your database
server. Be careful, deleting a database would loss all the data stored in the database.
Create Table
The CREATE TABLE statement is used for creating a new table. The syntax is –
SQL> CREATE TABLE table_name
(
Column_name column_data_type,
Column_name column_data_type,
Column_name column_data_type

);
Example
The following SQL statement creates a table named Employees with four columns –
SQL> CREATE TABLE Employees
(
Id INT NOT NULL,
Age INT NOT NULL,
First VARCHAR(255),
Last VARCHAR(255),
PRIMARY KEY ( id )
);

Drop Table
The DROP TABLE statement is used for deleting an existing table. The syntax is –
SQL> DROP TABLE table_name;
Example
The following SQL statement deletes a table named Employees –
SQL> DROP TABLE Employees;
INSERT Data
The syntax for INSERT, looks similar to the following, where column1, column2, and so on
represents the new data to appear in the respective columns –
SQL> INSERT INTO table_name VALUES (column1, column2, …);
Example
The following SQL INSERT statement inserts a new row in the Employees database created
earlier –
SQL> INSERT INTO Employees VALUES (100, 18, ‘Zara’, ‘Ali’);
SELECT Data
The SELECT statement is used to retrieve data from a database. The syntax for SELECT is –
SQL> SELECT column_name, column_name, …
FROM table_name
WHERE conditions;
The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as
the BETWEEN and LIKE operators.
Example
The following SQL statement selects the age, first and last columns from the Employees table,
where id column is 100 –
SQL> SELECT first, last, age
FROM Employees
WHERE id = 100;
The following SQL statement selects the age, first and last columns from the Employees table
where first column contains Zara –
SQL> SELECT first, last, age
FROM Employees
WHERE first LIKE ‘%Zara%’;

UPDATE Data
The UPDATE statement is used to update data. The syntax for UPDATE is –
SQL> UPDATE table_name
SET column_name = value, column_name = value, …
WHERE conditions;
The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as
the BETWEEN and LIKE operators.
Example
The following SQL UPDATE statement changes the age column of the employee whose id is 100

SQL> UPDATE Employees SET age=20 WHERE id=100;
DELETE Data
The DELETE statement is used to delete data from tables. The syntax for DELETE is –
SQL> DELETE FROM table_name WHERE conditions;
The WHERE clause can use the comparison operators such as =, !=, <, >, <=,and >=, as well as
the BETWEEN and LIKE operators.
Example
The following SQL DELETE statement deletes the record of the employee whose id is 100 –
SQL> DELETE FROM Employees WHERE

You might also like