0% found this document useful (0 votes)
36 views8 pages

SQL - 09 (JDBC)

JDBC (Java Database Connectivity) allows Java applications to connect to databases. It provides a standard interface for connecting to different database systems and allows SQL statements and results to be returned as Java objects. A JDBC driver implements the JDBC interface and provides database-specific connectivity. It translates Java methods into SQL and converts SQL results to Java objects. This allows Java code to interact with various databases in a standard way without needing database-specific code.

Uploaded by

priyobrato banik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

SQL - 09 (JDBC)

JDBC (Java Database Connectivity) allows Java applications to connect to databases. It provides a standard interface for connecting to different database systems and allows SQL statements and results to be returned as Java objects. A JDBC driver implements the JDBC interface and provides database-specific connectivity. It translates Java methods into SQL and converts SQL results to Java objects. This allows Java code to interact with various databases in a standard way without needing database-specific code.

Uploaded by

priyobrato banik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

JDBC:

======

--Java Database Connectivity:

---Java mostly used in industry to develop Business Application.

--common general things required in a business application:

1. maintain business data permanently in a secure and easily retrivable manner.

2. processing the data according to the business rule.

3.presenting ther data to the end-user in user-understandable format.

--in the above things the most important thing is persistant business data or
stored data.

--we store the business data for a typical business application inside a RDBMS.

Q/- Why Java DB communication is required ?

A/- DBMS are excellent in data storage in secure and easily retrivable manner,
but it is very poor in processing the data and presenting the data in user-
understandable
format.

--Java is excellent in processing the data and presenting the data, but it is very
poor in storing the data.

--so inorder to develop a powerfull business application we need to communicate


Java application with the DB.

Q/ How Java-DB communication is possible ?

A/- Java app can do any task only through method calls and objects. these method
calls and objects are not
directly understandable to the DB.
and DB can understand only sql statements, Java compiler does not accept the sql
syntax directly.

--inspite of heteregenious platform Java-DB communication is possible through JDBC.

translator
Chinese <----------------> Russian

JDBC driver s/w


Java app <----------------> RDBMS

--Jdbc is technology that enables any kind of Java application to communication


with any kind of DB s/w in a
standard manner.

--JDBC technlolgy is given by the Sun microsystem (Oracle corp.)

--This JDBC terchnology comes in the form of a specification(it is a documentation


which describes rules and guildlines to develop a "Jdbc driver s/w").

--this "Jdbc Driver s/w" is the implementaion of the JDBC specificaiton. which will
act as a translator to communicate our java appl with the DB s/w.

--JDBC Specification will be implemeted by the DB vendor or any 3rd party vendor
and develop jdbc driver s/w

MYSQL
ORCALE
POSTGRES
SQLSERVER
DB2
RATANDB------ driver s/w of Ratandb ------------Java application developer

--inorder to comunnicate java application and DB server, Java developer need the
jdbc driver s/w. and also jdbc api to perform the DB operation.

--jdbc api comes in form of 2 packages:

1.java.sql package
2.javax.sql package

--using this jdbc api we can work with jdbc driver s/w and can communicate our java
appl with the DB s/w.

--jdbc api comes along with the JDK installation.

--Jdbc driver comes in the form of jar file.

.java----.class---jar file

--java developer need to get/download the jdbc driver related jar file from net
and in order to work with that jar file , we need to set that file inside the
classpath of
our java application.

each DB s/w have thier own jdbc driver files:


ex:

mysql : mysql-connector.jar
oracle: Ojdbc6.jar
Postgres: Postrgres.jar

Jdbc Client:
==========

In Java-DB communication, Java application will act as client because Java


application
needs the service of DB server. so Java application is a JDBC client.
Resposiblilty of a JDBC client:
=======================
1.Requesting the DB connection.
2.Submitting the appropriate SQL statement to the DB server in the form of String.
3.Processing the Result given by the DB server.
4.Dealing with exception if any.
5.Managing the transaction whenever it is required.
6.Closing the connection once done with DB-operation.

JDBC Driver:
==========

--it is a translation s/w written in java according to the JDBC specification


(comes in the form of Jar file)

Responsibility of JDCB Driver s/w:


===========================
1. establising the connection.
2.receiving the JDBC method calls (java method calls ) and translate them into
DBMS understandable format(SQL) and forward them to the DB s/w.
3.Translating the DB s/w given result into the Java Format (java objects) and
returns that obj to the JDBC client.

Steps to connect our Java application with the DB server: (mysql)


==================================================

1. download/get the JDBC driver related jar file (mysql-connector.jar) and


set that jar file inside the classpath of our application.

2.Load the Driver related Main class into the memory.

3.Prepare the connection string.

4.establish the connection.

5.after performing the CRUD(insert,select,update,delete) operation close the


connection.

setting the jar file in the classpath of our application:


----------------------------------------------------------------

rightclick on the project---->build path---> configure build path--->libraries


tab--->classpath---->Add External jars(select the downloaded jar file) ----> apply
and close.

Demo.java:
---------------

package com.masai;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo {

public static void main(String[] args) {

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("problem with loding the driver main class");
}

String cs = "jdbc:mysql://localhost:3306/sb101db";

try {
Connection conn= DriverManager.getConnection(cs,"root","root");

if(conn != null)
System.out.println("connected");
else
System.out.println("not connected..");

}catch(SQLException ex) {
ex.printStackTrace();
}

Note: from Java 1.6 onwards loading the driver related main class is optional.

When forName(-) method executed 3 things happens in the background:

1. Driver related main class will be loaded into the memory.

2.Driver related main class object will be created.

3.that object will be registered with the DriverManager class.

ex:

try {

Driver d = new Driver();

DriverManager.registerDriver(d);
}
catch(SQLException e) {

}
--the above statements is written inside the static block of the Driver class.
and whenever any class will be loaded into the memory their static blocks
will be executed.

static and non static block in java:


===========================

package com.masai;

public class A {

{
System.out.println("inside non-static block of A");
}

static{
System.out.println("inside non-static block of A");
}

A(){
System.out.println("inside the constructor of A");
}

public void funA() {


System.out.println("inside funA of A");
}

public static void main(String[] args) {

System.out.println("inside main of A");

A a1 = new A();

a1.funA();

output:
inside static block of A
inside main of A
inside non-static block of A
inside the constructor of A
inside funA of A

---after loading all the static members and before starting the application from
main
jvm will execute all the static block one by one.

--whenever we create object of a class then constructor will be executed. before


constructor
execution will starts all the non-static blocks will be executed one by one.

ex2:
----

public class A {

static{
System.out.println("inside static block of A");

A a1 = new A();
a1.funA();
}

public void funA() {


System.out.println("inside funA of A");
}

ex2: inserting a record in a table:


--------------------------------------

package com.masai;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.cj.jdbc.Driver;

public class Demo {

public static void main(String[] args) {

try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("problem with loding the driver main class");
}

String cs = "jdbc:mysql://localhost:3306/sb101db";

try {
Connection conn= DriverManager.getConnection(cs,"root","root");

Statement st= conn.createStatement();


int x= st.executeUpdate("insert into student
values(10,'ram','delhi',780)");

if(x >0 )
System.out.println("inserted..");
else
System.out.println("not inserted");

}catch(SQLException ex) {
ex.printStackTrace();
}

You might also like