Date: 16-08-2024
SQL Concepts / Database Concepts
SQL stands for Structured Query Language.
SQL is set of Commands which are used to operate a database.
A Database is a collection of related tables.
A table is a collection of data in the form of rows and columns.
Example:
SQL Command Types:
❖ DDL (Data Definition Language) = create, drop, alter.
❖ DML (Data Manipulation Language) = insert, update, delete.
❖ DQL (Data Query Language) = select.
❖ TCL (Transaction Control Language) = commit, rollback.
Database Servers:
➢ MySQL (Oracle Corp)
➢ Oracle (Oracle Corp)
➢ DB/2 (IBM)
➢ SQl Server (Microsoft)
➢ Sybase (Microsoft)
Download and Install MySQL Database Server v8:
Configuring MySQL:
Default setting the parameters to:
▪ Port Number: 3306
▪ username: root
▪ password: root
• Creating a database => MySQL>create database Sourav(database-name);
• Use/Change Database => MySQL>use Sourav;
• Drop Database (delete the database) => drop database Sourav;
Datatypes
❖ int
❖ float
❖ char
❖ varchar
❖ date
Commands and Uses:
❖ create table: used to create table with specified columns.
Syntax:
o create table table-name (column1 datatype(size), column2
datatype(size), ... , column-n datatype(size));
❖ desc command: used to display structure of table.
▪ Ex: desc books;
❖ drop table command: used to delete table.
▪ Ex: drop table books(table-name);
❖ insert Command: used to insert records into the table.
Syntax:
▪ insert into table-name values (value1, value2, . . . , valuen);
▪ no of values = no of columns of the table.
▪ insert into table-name(column-1, column-2, . . ., column-n) values
(value1, value2, . . . , valuen);
▪ no of values = no of columns given in the command.
Ex:
▪ insert into books values(111, 'Java', 500);
▪ insert into books(bno,bname) values (222, 'xml');
❖ update command: used to modify/update the exsisting records
Syntax:
▪ update table-name set column=new-value, column=new-value, . . .
, column = new-value [where condition];
▪ [] => optional
Ex: update books set price = 600;
//for all records price will be updated to 600
update books set price = 800 where bno = 222;
❖ TCL Commands:
▪ Commit: used to save transactions (insert, update, delete)
▪ Ex: MySQL > commit;
▪ rollback: used to undo uncommitted transactions
▪ Ex: MySQL > rollback;
[Note: In MySQL, by default the transaction are autocomitted.]
▪ Setting autocommit mode to false / true:
▪ MySQL > set autocommit = 0/1;
❖ Delete command: used to delete the records of the table.
syntax: delete from table-name [ where condition];
Ex: delete from books where price >= 500 and price <= 700;
[Note: When autocomitted is off, after deleting if we rollback, we can
retrieve the data]
❖ select command: used to retrieve the records of the table.
Ex: select * from books; // * indicates all columns of table
▪ select bno,bname from books;
▪ select * from books where bno = 222;
❖ Alter table command: used to modify the structure of the table like the
following:
▪ rename the table
▪ add new columns
▪ modify existing columns
▪ rename columns
▪ drop columns
MySQL > alter table books rename to tempbooks;
MySQL > alter table tempbooks rename to books;
MySQL > alter table books add author varchar(10);
MySQL > alter table modify bname varchar(15);
MySQL > alter table books change bname bookname varchar(15);
MySQL > alter table books drop column author;
JDBC:
JDBC stands for Java Database Connectivity.
JDBC is an API which is used to connect to a
database from a Java Program.
Java Program - > JDBC API -> Database
Steps in JDBC:
1. import java.sql package
2. Load the Driver class
Class.forName("com.mysql.cj.jdbc.Driver");
o Driver is a .class file present in com.mysql.cj.jdbc package
3. Establish the connection
Connection con = DriverManager.getConnection("JDBC:
mysql://localhost:3306/java05”,” root”,”root");
o localhost => IP Address or Hostname of MySQL Server
o 3306 => port number where MySQL server is running
o java05 => database name
o root => username
o root => password
4. Create a statement
a. Statement - createStatement()
b. PreparedStatement - preparedStatement()
Ex:
▪ Statement st1 = con.createStatement();
▪ PreparedStatement st2 = con.prepareStatement("Select * from
employee");
5. Execute the statement
a. Boolean execute () - create, alter, drop, truncate (DDL)
b. int executeUpdate() - insert, update, delete (DML)
c. ResultSet executeQuery()- select (DQL)
Ex:
i. st1.executeQuery("select * from employee);
ii. st2.executeQuery();
[ Note: We can use any method to execute the SQL command but to
manage the return type we need to use the specific method.]
6. close the statement
st1.close();
7. Close the connection
con.close();
Download MySQL-connector-java-8.0.30.jar:
Google: MySQL connector java
▪ In order to connect to MySQL Database from Java Program, we need to set
the classpath/buildpath to "MySQL-connector-java-8.0.30.jar" into project
as follows:
Right click on Myproj -> Build path -> Configure Build Path -> Select tab
Libraries -> Select Classpath -> Click Add External Jars -> select "MySQL-
connector-java-8.0.30.jar" from the folder -> click open -> click Apply and
close.
▪ Jar (java archive): which is a collection of .class files in compressed format.
▪ Exporting/Creating Jar file:
Right click on MyProj folder (which we want to export) -> click Export ->
under Java, select JAR file -> specify select location, set name -> click on
finish.
CRUD Operations:
C - Create (Insert Command)
R - Read (Select Command)
U - Update (update Command)
D - Delete (delete Command)
PreparedStatement interface:
• If the same SQL command is executed multiple times in the program, then it
is suggested to use PreparedStatement.
• PreparedStatement is dynamic means runtime we can pass values to the
SQl command.