CH 6
CH 6
JDBC
Database Systems – an Introduction
Structured Query Language
Populating a database
Executing Queries
MySQL
MongoDB
PostgreSQL etc.
SQL commands are instructions. It is used to communicate with the
database. It is also used to perform specific tasks, functions, and queries of
data.
SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.
The following are two main type of SQL command
1. Data Definition Language (DDL) – It define and manage the structure of a
database. It deals with the creation, modification, and deletion of database.
Defines and manages database structure.
2. Data Manipulation Language - used for manipulating data within database
table. It allows users to query, insert, update, and delete data. Manipulates
and manages data within tables.
DDL changes the structure of the database like creating a table,
deleting a table, altering a table, etc.
The following command are DDL
Create – to create database and table.
Drop – to delete database.
Alter and other – modify an existing database object.
CREATE It is used to create data base and a new table in the
database.
SQL syntax: to create database
CREATE DATABASE databasename;
Example: CREATE DATABASE stud;
Syntax: to create table
CREATE TABLE table_name(COLUMN_NAMES DATATYPES []);
Example:
CREATE TABLE student(id int, name varchar(20), age int);
DROP: It is used to delete both database and the structure or
record stored in the table.
SQL syntax: to delete database
DROP DATABASE databasename;
DROP DATABASE stud;
Syntax: To delete a table permanently from memory.
DROP TABLE table_name
ALTER: It is used to alter the structure of the database. Used to add,
delete, or modify columns in an exiting table.
Syntax: To add a new column in the table
ALTER TABLE table_name ADD column_name data_type;
E.g. ALTER TABLE Customers ADD Email varchar(255);
Syntax: To drop or delete column in the table
ALTER TABLE table_name DROP COLUMN column_name;
E.g. ALTER TABLE Customers DROP COLUMN Email;
Syntax: To rename column name in the table
ALTER TABLE table_name RENAME COLUMN old_name to new_name;
DML commands are used to modify the database. It is
responsible for all form of changes in the database. It change
the content of database.
The following command are DDL
o Insert - Adds new data (rows) into a table.
o Update - Modifies existing data in a table
o Select - Retrieves data from one or more tables.
o Delete - Removes data (rows) from a table
INSERT: The INSERT statement is a SQL query. It is used to insert data into
the row of a table. To insert a new row into a table you must be your on schema
or INSERT privilege on the table.
SQL syntax:
INSERT INTO table_name(COLUMN1, COLUMN1) VALUES
(VALUE1, VALUE2);
Example
INSERT INTO student (name, age, department) VALUES ('John Doe', 30,
'Sales');
UPDATE command is used to update or modify the value of a
column in the table. To modify existing data, use the UPDATE
statement.
Syntax:
UPDATE table_name SET column1 = value1, column2 = value2 where condition;
E.g. UPDATE student SET age = 31 WHERE id = 1;
SELECT: This is the same as the projection operation of relational
algebra. To retrieve data from a table, use the SELECT statement. It is
used to select the attribute based on the condition described by WHERE
clause.
Syntax:
SELECT * FROM table_name;
SELECT column1, column2 FROM table_name;
In this statement which the asterisk (*) wildcard character indicates that
all columns from the student table should be retrieved.
E.g.
SELECT * FROM student WHERE department = ‘SW’;
The advantage of WHERE clause is to filter data. In the above example we
filter only SW department from student table.
DELETE is used to remove one or more row from a table. To
delete rows from the table, it must be in your schema or you
must have delete privilege. It used to delete existing records in
a table.
Syntax:
DELETE FROM table_name WHERE condition;
E.g.
DELETE FROM student where id = 1;
A SQL DELETE statement removes rows from a table.
We can use operator( Comparison, logical and other) in SQL
command instruction.
Comparison: =, >, <, <=, >= , !
Logical: AND, OR, NOT
LIKE operator –
E.g. SELECT * FROM table_name WHERE name LIKE ‘A%';
BETWEEN operator –
E.g. SELECT * FROM student WHERE age BETWEEN 25 AND 40;
SQL sorting
To sort the result set, use the ORDER BY clause
E.g. SELECT * FROM student ORDER BY age DESC;
JDBC is the connectivity of java and database.
It provide API that for updating, and querying relational database using SQL.
Java programs interact with databases using the Java Database Connectivity
(JDBC) API.
A JDBC driver enables Java applications to connect to a database in a
particular DBMS and allows you to manipulate that database using the JDBC
API.
Industry standard interface for connecting to relational databases from Java
Independent of any DBMS
You can use JDBC to interact with a database from within a Java program.
JDBC acts as a bridge from your code to the database, as shown below
JDBC consists of
JDBC API – it provide the set of interface and class to interact with the
database. The java.sql package contains classes and interfaces for JDBC
API. For example
JDBC API interface include Driver, Connection, Statement, Prepared
Statement Result Set interface and other.
JDBC API include Driver Manager class, Types class and other
JDBC Driver – it is a software component that allows Java application to
communicate with database.
JDBC
JDBC API
Database
JDBC Driver
The following are required to develop JDBC
JAVA editor(NEATBEAN, eclipse and other) and JDK
MYSQL connector
XAMP Server it has Apache, MYSQL, PHP and other
Connect
Query
Process
Results
Close
Driver is a software component that provides the facility to a
computer to communicate with hardware.
JDBC is the software component that enables java application to
interact with database.
Statically load driver with class class and its method forName();
Class.forName(“Driver Name”);
Use the jdbc.drivers system property
Every driver has its own name
◦ For example
Mysql - com.mysql.jdbc.Driver
Oracle - oracle.jdbc.driver.OracleDriver
Handled by DriverManager Object
DriverManager class provide the facility to create a connection
between a database and the appropriate driver .
To open a database connection we can call getConnection()
method of DriverManager class.
Syntax: getConnection() method accept JDBC url, username,
and password
Connection con = DriverManager.getConnection(url, username, password)
url like (“jdbc:mysql://localhost:3306/DBname” )
username “root”
password “”