Wa0010.
Wa0010.
© 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
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
Database Server
21
© Darshit
© Darshit
JDBC Driver: Type 3 (Java Protocol)
Local Computer Middleware Server
Type 3
JDBC-Net pure Java JDBC Type 4 Driver
Database Server
24
© Darshit
JDBC Driver: Type 4 (Database Protocol)
Local Computer
Java Application
Application Code
Type 4
100% Pure Java Local
DBMS
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");
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
2. Get methods Used to view the data in the columns of the current row
3. Update methods Used to update the data in the columns of the current
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.
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.
72
Types of ResultSet
Type Description
73
Concurrency of ResultSet
Concurrency Description
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