0% found this document useful (0 votes)
27 views

JDBC

JDBC is an API that allows Java programs to interact with databases. It provides interfaces and classes that allow a Java program to connect to a database, execute queries and process the results. The key components of JDBC include the JDBC API, driver manager, test suite and bridge drivers. It supports connecting to all major databases like Oracle, SQL Server and MySQL. Programs use JDBC to load drivers, establish connections, create statements, execute queries and process result sets.

Uploaded by

Hari Chandrudu M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

JDBC

JDBC is an API that allows Java programs to interact with databases. It provides interfaces and classes that allow a Java program to connect to a database, execute queries and process the results. The key components of JDBC include the JDBC API, driver manager, test suite and bridge drivers. It supports connecting to all major databases like Oracle, SQL Server and MySQL. Programs use JDBC to load drivers, establish connections, create statements, execute queries and process result sets.

Uploaded by

Hari Chandrudu M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

JDBC

(Java Database Connectivity)

CREATED BY K. VICTOR BABU


JDBC is an API(Application programming interface)

which is used in java programming to interact with

databases.

CREATED BY K. VICTOR BABU


Purpose of JDBC
• Enterprise applications that are created using the JAVA EE technology
need to interact with databases to store application-specific information.

• So, interacting with a database requires efficient database connectivity


which can be achieved by using the ODBC(Open database connectivity)
driver.

• This driver is used with JDBC to interact or communicate with various


kinds of databases such as Oracle, MS Access, Mysql and SQL server
database.

CREATED BY K. VICTOR BABU


Components of JDBC
1. JDBC API: It provides various methods and interfaces for

easy communication with the database.

It provides two packages as follows which contains the java

SE and java EE platforms to exhibit WORA(write once run

everywhere) capabilities.

java.sql.*;

javax.sql.*;

It also provides a standard to connect a database to a client


CREATED BY K. VICTOR BABU
application.
2. JDBC Driver manager: It loads database-specific driver in an
application to establish a connection with a database. It is
used to make a database-specific call to the database to
process the user request.

3. JDBC Test suite: It is used to test the operation(such as


insertion, deletion, updation) being performed by JDBC
Drivers.

CREATED BY K. VICTOR BABU


4. JDBC-ODBC Bridge Drivers: It connects database drivers to
the data base. This bridge translates JDBC method call to the
ODBC functioncall. It makes the use of sun.jdbc.odbc
package that includes native library to access ODBC
characteristics.

CREATED BY K. VICTOR BABU


2 tier and 3 tier Architectures for Data Access

CREATED BY K. VICTOR BABU


Path:

C:\oraclexe\app\oracle\product\10.2.0\
server\jdbc\lib

CREATED BY K. VICTOR BABU


Steps to develop JDBC application

1. Import the package


2. Load and Register driver
3. Establishing connection
4. Create the statements
5. Execute the queries
6. Process Result set
7. Close connection

CREATED BY K. VICTOR BABU


Creating a table in JDBC:

Statement stmt = con.createStatement


("create table product (pid number(4) primary key,pname
varchar2(20),price number(10,2))";

int n = stmt.executeUpdate(vsql);

CREATED BY K. VICTOR BABU


Inserting a single row into a table:

Statement st=con.createStatement();
int i=0;
i=st.executeUpdate ("insert into product
values(107,'hhh',10.9)");

CREATED BY K. VICTOR BABU


Update a record in table using JDBC:

Statement st=con.createStatement();
int i=0;
i=st.executeUpdate("update product set price=123.45 where
pid=107");

CREATED BY K. VICTOR BABU


Inserting multiple number of records into a table using JDBC:

String vsql = "insert into product values(?,?,?)";

PreparedStatement pstmt = con.prepareStatement(vsql);

Scanner sc = new Scanner(System.in);


System.out.print("Enter how records:");
int nr = sc.nextInt();
System.out.println("No of record you need to enter:"+nr);
for(int i=1;i<=nr;i++){
System.out.println("Enter product id:");
id = sc.nextInt();

CREATED BY K. VICTOR BABU


System.out.println("Enter product name:");
name = sc.next() + sc.nextLine();
System.out.println("Enter product price:");
price = sc.nextDouble();

//storing data into positional parameters


pstmt.setInt(1,id);
pstmt.setString(2,name);
pstmt.setDouble(3,price);

//execute the query


pstmt.executeUpdate();

CREATED BY K. VICTOR BABU


Types of Statements in JDBC

CREATED BY K. VICTOR BABU


QUIZ

In order to transfer data between a database and an


application written in the Java programming language, the
JDBC API provides which of these methods?
A. Methods on the ResultSet class for retrieving SQL SELECT results as
Java types.
B. Methods on the PreparedStatement class for sending Java types as
SQL statement parameters.
C. Methods on the CallableStatement class for retrieving SQL OUT
parameters as Java types.
D. All mentioned above

ANSWER: D

CREATED BY K. VICTOR BABU


Which JDBC type represents a "single precision" floating point
number that supports seven digits of mantissa?
A. REAL
B. DOUBLE
C. FLOAT
D. INTEGER

ANSWER: A

CREATED BY K. VICTOR BABU


Which method is used for retrieving streams of both ASCII and
Unicode characters is new in the JDBC 2.0 core API?
A. getCharacterStream
B. getBinaryStream
C. getAsciiStream
D. getUnicodeStream

ANSWER: A

CREATED BY K. VICTOR BABU


Any Queries!

CREATED BY K. VICTOR BABU

You might also like