0% found this document useful (0 votes)
13 views26 pages

Wa0010.

The DriverManager class manages JDBC drivers and is responsible for establishing connections to databases. The Connection interface represents a connection to a specific database. The Statement interface is used to execute a SQL statement and return the results as a ResultSet. The ResultSet interface represents the results of a query and allows accessing the data through a cursor. JDBC drivers implement the Driver interface and are responsible for communicating with the database.

Uploaded by

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

Wa0010.

The DriverManager class manages JDBC drivers and is responsible for establishing connections to databases. The Connection interface represents a connection to a specific database. The Statement interface is used to execute a SQL statement and return the results as a ResultSet. The ResultSet interface represents the results of a query and allows accessing the data through a cursor. JDBC drivers implement the Driver interface and are responsible for communicating with the database.

Uploaded by

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

1

Darshit , 02-07-2023, 23:06:19

© Darshit
© Darshit
© Darshit
JDBC Driver: Type 1 (JDBC-ODBC Driver)
Local Computer

Java Application
DB Vendor
Application Code Driver

Type 1
ODBC Local
JDBC ODBC Bridge
Driver DBMS

Vendor Specific Protocol Network Communication

Database Server
18
© Darshit
JDBC Driver: Type 2 (Native Code Driver)
Local Computer

Java Application
DB Vendor Driver
Application Code

Type 2
Native API Local
DBMS

Vendor Specific Protocol Network Communication

Database Server
21
© Darshit
© Darshit
JDBC Driver: Type 3 (Java Protocol)
Local Computer Middleware Server

Java Application JDBC Type 1 Driver


Application Code
JDBC Type 2 Driver

Type 3
JDBC-Net pure Java JDBC Type 4 Driver

Vendor Specific Protocol Network Communication

Database Server
24
© Darshit
JDBC Driver: Type 4 (Database Protocol)
Local Computer

Java Application

Application Code

Type 4
100% Pure Java Local
DBMS

Vendor Specific Protocol Network Communication

Database Server
28
© Darshit
© Darshit
Q-4 Program for Simple, Prepared and Callable Statement
1. Simple
1. import java.sql.*;
2. public class ConnDemo {
3. public static void main(String[] args) {
4. try {
5. Class.forName("com.mysql.jdbc.Driver");
Database name
6. Connection conn= DriverManager.getConnection
7. ("jdbc:mysql://localhost:3306/gtu","root",”pwd");
8. Statement stmt = conn.createStatement();
9. ResultSet rs = stmt.executeQuery("SELECT * from diet");
10. while(rs.next()){
11. System.out.print(rs.getInt(1)+"\t"); Table name
12. System.out.print(rs.getString(“Name”)+"\t");
13. System.out.println(rs.getString(3));
14. }//while
15. stmt.close();
16. conn.close();
17. }catch(Exception e){System.out.println(e.toString());
18. }//PSVM }//class

56
2. Prepared Statement
1. import java.sql.*;
2. public class PreparedInsert {
3. public static void main(String[] args) {
4. try {
5. Class.forName("com.mysql.jdbc.Driver");
6. Connection conn= DriverManager.getConnection
7. ("jdbc:mysql://localhost:3306/gtu", "root",“pwd");
8. String query="insert into dietstudent values(?,?,?,?)";
9. PreparedStatement ps=conn.prepareStatement(query);
10. ps.setString(1, "14092"); //Enr_no
11. ps.setString(2, "abc_comp"); //Name
12. ps.setString(3, "computer"); //Branch
13. ps.setString(4, "cx"); //Division
14. int i=ps.executeUpdate();
15. System.out.println("no. of rows updated ="+i);
16. ps.close();
17. conn.close();
18. }catch(Exception e){System.out.println(e.toString());} }//PSVM
}//class
62
3.Callable Statement

1. import java.sql.*;
2. public class CallableDemo {
3. public static void main(String[] args) {
4. try {
5. Class.forName("com.mysql.jdbc.Driver");
6. Connection conn= DriverManager.getConnection
7. ("jdbc:mysql://localhost:3306/gtu", "root",“pwd");

8. CallableStatement cs=conn.prepareCall("{call gettitle(?,?)}");


9. cs.setInt(1,1201);
10. cs.registerOutParameter(2,Types.VARCHAR); Procedure Name
11. cs.execute();
12. System.out.println(cs.getString(2));

13. cs.close();
14. conn.close();
15. }catch(Exception e){System.out.println(e.toString());}
16. }//PSVM
17. }//class

67
Method: ResultSet
Q-5 Explain ResultSet with its all methods, types, etc

Categories

1. Navigational methods Used to move the cursor around.

2. Get methods Used to view the data in the columns of the current row

being pointed by the cursor.

3. Update methods Used to update the data in the columns of the current

row. The updates can then be updated in the underlying

database as well.

69
ResultSet: Navigational methods
boolean first() Moves the cursor to the first row.
throws SQLException
boolean last() Moves the cursor to the last row.
throws SQLException
boolean next() Moves the cursor to the next row. This method
throws SQL Exception returns false if there are no more rows in the result
set.
boolean previous() Moves the cursor to the previous row. This method
throws SQLException returns false if the previous row is off the result set.
boolean absolute(int row) throws Moves the cursor to the specified row.
SQLException
boolean relative(int row) throws Moves the cursor the given number of rows forward
SQLException or backward, from where it is currently pointing.
int getRow() Returns the row number that the cursor is pointing to.
throws SQLException

70
ResultSet: Get methods
int getInt(String columnName) Returns the int in the current row in the column
throws SQLException named columnName.

int getInt(int columnIndex) throws Returns the int in the current row in the specified
SQLException column index. The column index starts at 1,
meaning the first column of a row is 1, the second
column of a row is 2, and so on.

String getString(String columnLabel) Retrieves the value of the designated column in


throws SQLException the current row of this ResultSet object as
a String in the Java programming language.

String getString(int columnIndex) Retrieves the value of the designated column in


throws SQLException the current row of this ResultSet object as
a String in the Java programming language.

71
ResultSet: Update methods
void updateString(int col_Index, String s) Changes the String in the specified column
throws SQLException to the value of s.

void updateInt(int col_Index, int x) Updates the designated column with


throws SQLException an int value.

void updateFloat(int col_Index, float x) Updates the designated column with


throws SQLException a float value.

void updateDouble(int col_Index,double x) Updates the designated column with


throws SQLException a double value.

72
Types of ResultSet
Type Description

ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the


result set. Default
Type
ResultSet.TYPE_SCROLL_INSENSITIVE The cursor can scroll forward and backward,
and the result set is not sensitive to
changes made by others to the database
that occur after the result set was created.

ResultSet.TYPE_SCROLL_SENSITIVE The cursor can scroll forward and backward,


and the result set is sensitive to changes
made by others to the database that occur
after the result set was created.

73
Concurrency of ResultSet
Concurrency Description

ResultSet.CONCUR_READ_ONLY Creates a read-only result set.


Default
ResultSet.CONCUR_UPDATABLE Creates an updateable result set. Type

74
ResultSet
try {
Statement stmt = conn.createStatement(
ResultSet Type
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}
catch(Exception ex)
{ ResultSet Concurrency

....
}

75
Q-6 Discuss following terms: (1) Driver Manager Class (2) Connection
(3) Statement (4) Resultset (5)Driver with respect to JDBC
Driver Manager Class:
The DriverManager class in JDBC is responsible for managing and coordinating the available
database drivers. It acts as an intermediary between the Java application and the appropriate
JDBC driver needed to connect to a specific database.
Connection:
The Connection interface in JDBC represents a connection to a specific database. It provides
methods to establish a connection, manage transactions, and execute SQL statements.
Statement:
The Statement interface in JDBC is used to execute SQL statements and retrieve results from a
database. It represents a simple SQL statement without any parameters or it can be
parameterized using placeholders.
ResultSet:
The ResultSet interface in JDBC represents a set of data retrieved from a database after
executing a query. It provides methods to navigate through the result set and retrieve data from
individual rows and columns.
Driver with respect to JDBC:
A JDBC driver is a software component that allows Java applications to interact with a specific
database. It acts as a bridge between the application and the database, translating JDBC API
calls into the DBMS-specific protocol. There are different types of drivers, including the
JDBC-ODBC Bridge, Native-API, Network Protocol, and Thin drivers, each with its own
characteristics and requirements. Choosing the

1
appropriate driver depends on factors such as the DBMS used, deployment environment, and
performance considerations.
Q-7 Differentiate between java.sql and javax.sql package

84

You might also like