Sample Queries
Sample Queries
-------------------------------------------------------------
Sample Queries:
1) I want to create Course table with columns
Course_id course_name Duration start_date
STUDENT
ROLL_NUMBER SECTION NAME MARKS
101 A SUNIL 699
102 A ANIL 599
101 B KETHAN 489
102 B SURAJ 571
Date: 22/02/2024
Introduction to JDBC:
Date : 23/02/2024
What are the issues with files?
1. Searching for a specific data in a file will be a time
consuming task.
2. Same data can be stored multiple times in a file. So it
leads to redundant data/duplicate data. The problem
is memory space will be wasted.
3. When data is stored across multiple files then
retrieving the related information/data from the files
is a tedious task.
4. When data exist in multiple files, if the data is
modified at one file and not modified at another file
then it leads to data inconsistency.
5. The data stored in files is not secured. Because, once
a file a opened all the data of that file will be visible.
All the above issues we can overcome with the
Databases.
CRUD operations.
C – create ( creating the data / inserting the data)
R – Read ( retrieving the data/selecting the data)
U – Update ( updating the data/modifying the data)
D – Delete ( deleting the data / removing the data)
JDBC Technology:
Technology refers to the tools and techniques to
solve the problems.
API Java Library or Java API both are same.
Java API refers to a collection of
pre-defined/pre-existing programs given by the
programming language to develop user-defined
applications.
Date: 24/02/2024
JDBC Technology refers to a Java API that enables Java
applications to interact with the Databases.
A Java Application will execute SQL queries on a
database and gets the results from the database using
JDBC API.
Java Applications uses JDBC Drivers as intermediaries
to connect with database and to execute the queries.
JDBC Drivers will translate the JDBC calls to Database
calls, executes queries and retrieves the result from the
database to the Java Application.
JDBC Technology has provided 4 types of drivers.
1. java.sql
2. javax.sql
The difference between java.sql and javax.sql is,
javax.sql contains extended/enhancement for the
features that are available java.sql package.
Date: 26/02/2024
The Three key components of JDBC technology are,
1. JDBC API
2. DriverManager
3. Driver
DriverManager is a utility class from JDBC Api, which
manages the drivers which are loaded into the JVM.
A Java program provides a database URL to the
DriverManager and based on the URL, it will pickup
appropriate driver and establishes connection with a
database.
A Java program has to call getConnection() method of
DriverManager class to pass Database URL, username
and password.
Driver applications/software will be installed along with
the Database software, in the form of jar files.
Jar stands for Java Archive files.
A jar file contains a collection of programs/classes.
A Java programmer has to know the connection
properties to establish a connection with a database.
1. Driver class name
2. Database url
3. Username
4. Password
For ex, to connect with Oracle Database, the connection
properties are,
Driver classname-> oracle.jdbc.driver.OracleDriver
Database url-> jdbc:oracle:thin:@<host>:1521:<listener>
jdbc:oracle:thin:@localhost:1521:XE
Note: XE is the listener for Oracle Express Edition
ORCL is the listener for Oracle Enterprise Edition
How to find the listener if you don’t know?
Open windows Run Type Services.exe find
OracleServiceXE / OracleServiceORCL
Username -> system
Password -> tiger
For ex, the connection properties to connect with
MySQL are,
Driver classname-> com.mysql.cj.jdbc.Driver
Database url -> jdbc:mysql://<host>:<port>/<database>
Jdbc:mysql://localhost:3306/test
Username -> root
Password -> root
Create Statement:
call createStatement() method of Connection object. So
that it will create a Statement object.
Statement stmt = conn.createStatement();
Date : 27/02/2024
Note: DriverManager is a class, Connection and
Statement are the interfaces from JDBC API.
For the interfaces provided in JDBC API, the
implementation classes exist in Driver
software/application.
Executing queries:
For executing the queries, mostly we need to call one of
the below methods of Statement object.
1. executeUpdate(String query)
2. executeQuery(String query)
For insert/update/delete operations, call
executeUpdate() method.
For select operations, call executeQuery() method.
executeUpdate() method returns an integer value, which
indicates the number of rows effected in the database.
For ex:
String query = “ DELETE FROM EMP WHERE SAL >
5000”;
int count = stmt.executeUpdate(query);
When you run a select query, from database the
program receives the records. These records must be
stored into a ResultSet object.
ResultSet is an interface, and its implementation is exist
in driver software/application.
For ex,
String query = “ SELECT * FROM EMP WHERE DEPTNO =
20 “;
ResultSet rs = stmt.executeQuery( query );
example program
Open a notepad and type the following.
// This program is to insert one record into the table
EMP
import java.sql.*;
class Insert_Emp {