JDBC Introduction
JDBC Introduction
Jdbc Introduction
JDbc stands for Java Database Connectivity.JDBC is API to connect and execute the
query with the database.
There are four type of drivers:
01)JDBC-ODBC Bridge Driver.
02)Native Driver
03)Network Protocal Driver
04)Thin Driver
We can use JDBC API to access data stored in any relational database.we can
save,update,delete and fetch or retrive data from database.
JDBC Driver:
01)JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database.The JDBC-
ODBC bridge driver converts JDBC method calls into ODBC
function calls.
Oracle does't support this driver from java 8.Oracle recommends that you are useing
JDBC drivers provided by the vendor.
04)Thin driver:
This driver converts jdbc calls into vendor database protocal.
04)Execute query
executeQuery() method of Statement interface is used to execute queries to
database.
Eclipse
type: IDE gor java
version:2024-03
vendor :: Eclipse Organization
open source(free IDE)
variants :: Eclipse SDK(only for standalone apps development)
Eclise JEE(for standalone, advanced applications development)
Installation :: Download as zip file and extract the zip file installation
Write a jdbc App to establish connection with Oracle Database s/w using type4 jdbc
driver s/w.
package com.fg.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
if(connection==null)
System.out.println("Connection is not established with the
database software");
else
System.out.println("Connection is established with the database
software");
}
}
(2)The pickup jdbc driver software establish the connection with "orcl" logical DB
of oracle DB s/w that
is running on 1521 port number of current computer.In that process it uses the
given dbuser name and password for
authentication.
(3)DriverManager.getConnection(-,-) returns jdbc connection object back to
application representing the
connectivity between java app and Database software.we can use that jdbc connection
object to check
wheather connection with database software established or not.
Write a Java App having jdbc code to get all the records of STUDENT db table.
step1) keep the following software setup ready
jdk 1.8+
oracle 11g
step2) create db table "Student" having records using SQL promt or sql plus
start button -->search for SQL --> Run SQL Command Line -->
sql>connect
Enter user-name: scott
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table student(sno number(5) primary key, sname varchar2(20), sadd
varchar2(20), avg float);
Table created.
1 row created.
1 row created.
1 row created.
step3) Develop Jdbc App (java app + jdbc code) to get records from "student" table
of oracle db s/w.
To get all records of "student" table, we need to use "SELECT * FROM
STUDENT" SQL Query which is SELECT
SQL Query..So use st.executeQuery(-) method to send and execute this SQL
Query in Db s/w.
package com.fg.jdbc;
Assignment1:Write a JDBC App to get records from DEPT table oracle db s/w?(default
db table of oracle).
write a jdbc app to insert record into student db table by collecting the values
from enduser.
package com.fg.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class InsertTest {
public static void main(String[] args) {
Scanner sc=null;
Connection con=null;
Statement st=null;
try {
//read input values
sc=new Scanner(System.in);
int no=0;
String name=null,addrs=null;
float avg=0.0f;
if(sc!=null) {
System.out.println("Enter student number ::");
no=sc.nextInt();//gives 1001
System.out.println("Enter student name ::");
name=sc.next();//gives raja
System.out.println("Enter student address");
addrs=sc.next();//gives hyd
System.out.println("Enter student avg ::");
avg=sc.nextFloat();//gives 90.6
}//if
//convert input values as required for the sql query
name="'"+name+"'";//gives 'raja'
addrs="'"+addrs+"'";//gives 'hyd'
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","ti
ger");
//create Statement object
if(con!=null)
st=con.createStatement();
//prepare sql query
//insert into values(1001,'raja','hyd',90.6)
String query="INSERT INTO STUDENT
VALUES("+no+","+name+","+addrs+","+avg+")";
System.out.println(query);
=>if u r getting connectivity issue with oracle Db s/w then make sure the following
two services of oracle
in running mode
write a JDBC App to update student addrs and avg based on the given student no?
package com.fg.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","ti
ger");
when we SQL Query to Db s/w from front end app like Java App , the Db s/w performs
3 operations on the SQL
Query
a)Parse/compile SQL Query
b)execute SQL Query
c)Fetch SQL Query result
1.Parse/compile
=>Here the received SQL Query will be splitted into multiple tokens(pieces)to
verify the syntax of SQL query
by compilation process.
select * from student
2.Execute
=>The parsed/compiled SQL Query will be executed to manupulate the DB tables data
or to get records from Db tables.
3.Fetch
=>The result/output of executed SQL Query will be collected/fetched and will be
given to Client app/front
end app.
=>Simple Statement is not suitable for executing same sql query for multiple times
either with same input values
or with different input values.
=>In railways ticket reservation application, there will be need of executing same
insert sql query for
multiple times with different passenger details.
=>In Google search ,there will be need of executing same select query having the
same trending topic name as
the input value.
10,00,000 ticket booking in railway ticket reservation app for passengers(Using
Simple Statement object):
a)=>10,00,000 times same INSERT Query goes to Db s/w with
different input values :: 10,00,000 * 0.01 secs= 10,000 secs
b)=>10,00,000 times same INSERT Query will be
parsed/compiled in Db s/w for multiple times :: 10,00,000 * 0.01 secs=
10,000 secs
c)=>10,00,000 times same INSERT Query will be
executed in Db s/w for multiple times :: 10,00,000 * 0.01 secs= 10,000
secs
d)=>10,00,000 times same INSERT Query output will be
fetched out from Db s/w for multiple times :: 10,00,000 * 0.01 secs=
10,000
Performing (a),(b) operations on the same SQL Query for multiple times is really
unnecessary but perform
(c),(d) operations on the same multiple times is really required..But while working
simple statement object we
can send only raw SQL Query(query with input values) to Db s/w every time..So the
query has to go through
parse,execute, fetch operations every time.
what we want is performing (a),(b) operations only for 1 time with out input values
and performing(c),(d)
operations for multiple times with different sets input values...For this we need
to use PreparedStatement
objects that supports pre-compiled SQL Queries.
//dynamic sql query(query with dynamic inputs nothing but qyery with params)
select * from student where sno=?
insert into student values(?,?,?,?)
note: the pre-compiled SQL Query can be the static SQL Query or dynamic SQL Query.
=>Simple statement obj deals with only static SQL Queries
=>PreparedStatement obj deals with both static and dynamic SQL Queries as
pre-compiled SQL Queries.
=>Simple Statement object makes the Db s/w to compile the SQL Query just
before execution of the SQL
Query.(It is like current ticket booking)
=>PreparedStatement object makes Db s/w to compile the SQL Query irrespective
of wheather it will be
executed or not(It is like advanced ticket booking)
30-Apr-24
Write a JDBC app to insert "n" no.of student details in DB table.
=>To insert "n" student details collected from Enduser we need to execute the
INSERT SQL Query for "n"times..
For that JDBC PreparedStatement object is required to use.
package com.fg.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","ti
ger");