0% found this document useful (0 votes)
130 views6 pages

Unit 5 Notes On Ajp 22517 Part 1

Uploaded by

gaytrinaphade
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)
130 views6 pages

Unit 5 Notes On Ajp 22517 Part 1

Uploaded by

gaytrinaphade
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/ 6

Unit V – Interacting with Database

TEACHING HOURS – 12 TOTAL MARKS – 12

Course Outcome (CO-5) – Develop programs using database.

1. Explain JDBC and ODBC.


Explanation -
JDBC:
 JDBC (Java Database Connectivity) is an API that allows Java applications to interact with
databases.
 The classes and interfaces of JDBC allow the application to send requests made by users
to the specified database.
 JDBC uses Structured Query Language (SQL) to send access requests to the database,
and then returns the results through a similar interface.
 JDBC can access a variety of data sources, including relational databases, spreadsheets,
and flat files.
 The JDBC API is made up of two packages: java.sql and javax.sql.
ODBC:
 Open Database Connectivity (ODBC) is a protocol that allows applications to access data
from a variety of database management systems (DBMSs).
 ODBC is an open standard Application Programming Interface (API) that's independent
of both operating systems and DBMSs.
Here are some benefits of ODBC:
 Interoperability: ODBC allows applications to access data from different DBMSs using a
single interface.
 Application independence: ODBC applications are independent of the DBMS they access.
 Multiple databases: ODBC applications can access data from multiple databases at the
same time.
 Efficiency: ODBC is an efficient and cost-effective way to read and write data between
databases.

2. Explain different types of JDBC Architecture.


Explanation -
JDBC API supports both two-tier and three-tier processing models for database access.
Two-tier Architecture:
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
In the two-tier model, a Java applet or application talks directly to the data source. This
requires a JDBC driver that can communicate with the particular data source being accessed.
A user's commands are delivered to the database or other data source, and the results of
those statements are sent back to the user. The data source may be located on another
machine to which the user is connected via a network. This is referred to as a client/server
configuration, with the user's machine as the client, and the machine housing the data source
as the server. The network can be an intranet, which, for example, connects employees
within a corporation, or it can be the Internet.

Three-tier Architecture:
In the three-tier model, commands are sent to a "middle tier" of services, which then
sends the commands to the data source. The data source processes the commands and sends
the results back to the middle tier, which then sends them to the user. Advantage o this
architecture is that it simplifies the deployment of applications.

Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
3. Display the hierarchy of all classes and interfaces provided by JDBC API.
Explanation -

DriverManager: Used to register and manage JDBC drivers.


Connection: Represents a connection to a database.
Statement: Used to execute SQL queries.
PreparedStatement: A precompiled SQL statement for improved performance and security.
CallableStatement: Used to execute stored procedures.
ResultSet: Represents the result set of a query.

4. Explain all the steps required to establish connection with database.


Explanation:
Step 1. Load the Driver.
Class.forName("com.mysql.cj.jdbc.Driver"); // For MySQL
Step 2. Establish a Connection.
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
Re
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";
Connection conn = DriverManager.getConnection(url, username, password);
Step 3. Create a Statement.
Statement stmt = conn.createStatement();
Step 4. Execute a Query.
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Step 5. Process the Result Set.
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + ": " + name);
}
Step 6. Close Resources.
rs.close();
stmt.close();
conn.close();

====================== Sample JDBC Program===========================


import java.sql.*;
class Mydatabase {
public static void main(String[] args) {
try {
// Load the JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");

// Establish the connection


String url = "jdbc:mysql://localhost:3306/college";
String username = "root";

Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
String password = "Admin@123";
Connection con = DriverManager.getConnection(url, username, password);

// Create a statement
Statement stmt = con.createStatement();

// Execute a query
ResultSet rs = stmt.executeQuery("SELECT * FROM students");

// Process the result set


while (rs.next()) {
System.out.println(rs.getInt("rollno"));
System.out.println(rs.getInt("name"));
System.out.println(rs.getInt("class"));
System.out.println(rs.getInt("branch"));

// Close the connection


rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate

You might also like