Ilovepdf Merged
Ilovepdf Merged
ORIENTED PROGRAMMING
(223 CCS-3)
CHAPTER – 5
JDBC BASICS
Prepared By:
Dr. Rincy Merlin Mathew
Introduction to JDBC
- JDBC stands for Java Database Connectivity, allowing
Java programs to communicate with databases via SQL,
facilitating data retrieval and manipulation.
- JDBC acts as a bridge between the Java applications and
databases, providing a standardized way to interact with
various database systems.
- Through JDBC, Java developers can execute SQL queries,
retrieve query results, update data, and manage
transactions seamlessly.
PRESENTATION TITLE 2
Overview of JDBC and
Architecture
- JDBC serves as a crucial bridge between Java applications
and databases, facilitating seamless communication for data
retrieval and manipulation.
- Understanding the JDBC architecture is essential for
developers to effectively interact with databases and
execute SQL queries.
- JDBC components like the API and driver manager play
key roles in enabling Java programs to interact with various
database systems.
JDBC Components
- The JDBC API defines interfaces for establishing database
connections and executing SQL queries within Java
applications.
- JDBC driver manager manages different JDBC drivers,
allowing applications to seamlessly interact with a variety of
databases.
- These components work together to provide a
standardized and efficient way for Java programs to
communicate with diverse database systems.
JDBC Driver Types with
Diagrams
- JDBC Driver Type 1, known as the JDBC-ODBC bridge,
offers easy integration but lacks performance as it converts
JDBC calls to ODBC calls.
- JDBC Driver Type 2, a Native API driver, enhances
performance by converting JDBC calls directly into the API
used by the database.
- JDBC Driver Type 3, a Middleware driver, facilitates
platform independence by using a middleware server to
convert JDBC calls into a vendor-specific protocol.
Basic SQL Commands in JDBC
- Basic SQL commands like SELECT, INSERT, UPDATE,
and DELETE are fundamental for database operations in
JDBC, allowing data retrieval, insertion, modification, and
deletion.
- Executing SELECT queries in JDBC enables developers to
retrieve specific data from databases based on specified
conditions, providing valuable information for Java
applications.
- INSERT, UPDATE, and DELETE commands in JDBC
facilitate adding new records, updating existing data, and
removing unwanted information from database tables
programmatically.
Using SQL Commands for JDBC
Connectivity
- SQL commands are essential in JDBC for executing
operations like SELECT, INSERT, UPDATE, and DELETE,
enabling data retrieval, modification, and management
seamlessly.
- Practical implementation of SQL commands in JDBC
scenarios showcases how developers can interact with
databases, retrieve specific data, and handle transactions
effectively.
- Understanding and utilizing SQL commands in JDBC
connectivity is crucial for Java developers to streamline
database interactions and enhance application functionality.
JDBC Connection Setup and
Connection Pooling
- To set up JDBC connections, specify the database URL,
username, and password to establish seamless
communication between Java applications and the database
server.
- Connection pooling optimizes resource usage by reusing
connections, minimizing overhead, and enhancing
application performance and scalability in JDBC
environments.
Management of JDBC
Connections
- Proper management of JDBC connections involves tasks
like opening, closing, and exception handling to ensure
efficient resource utilization and prevent memory leaks.
- Efficient connection management is crucial in JDBC
applications to maintain proper communication with
databases, enhancing performance and ensuring data
integrity.
- Handling exceptions during connection management in
JDBC is essential for robust application functionality,
preventing disruptions and ensuring reliable database
interactions.
Executing SQL with JDBC
- Executing SQL statements with JDBC allows developers to
interact with databases effectively, handling query execution
for data retrieval and manipulation.
- JDBC result sets play a crucial role in fetching query
results, enabling developers to iterate through data retrieved
from databases seamlessly.
- Understanding JDBC data types is essential for ensuring
compatibility and accurate data representation between Java
applications and databases during SQL execution.
JDBC Result Sets
- JDBC result sets are essential for fetching query results
from databases, enabling Java applications to iterate
through data efficiently for processing.
- Iterating through result sets in JDBC allows developers to
access and retrieve data from database queries accurately
and effectively.
- Efficient handling of JDBC result sets ensures that Java
applications can process query results seamlessly,
enhancing data manipulation and utilization.
JDBC Data Types
- JDBC data types encompass primitive and SQL data types
for seamless data handling between Java and databases,
ensuring compatibility and accurate data representation in
JDBC operations.
- Data type mapping is crucial in JDBC to maintain data
integrity and consistency, allowing for smooth data transfers
and interactions between Java applications and databases.
SQL Syntax in JDBC
- SQL syntax in JDBC is vital for executing database
operations like SELECT, INSERT, UPDATE, and DELETE,
enabling seamless data retrieval and manipulation in Java
applications.
- Examples demonstrate how SQL statements are
programmatically used in JDBC to interact with databases,
showcasing the practical implementation of database
operations.
JDBC Environment Setup
- Download the specific JDBC driver for your database
system and include it in the project's classpath to establish a
connection between Java and the database.
- Configure the JDBC driver by specifying the database
URL, username, and password for seamless communication
between Java applications and the database server.
- Ensure all required libraries and dependencies for JDBC
connectivity are correctly included and managed within the
project environment for a smooth setup.
JDBC Statement Objects
- Statement objects like PreparedStatement and
CallableStatement in JDBC enable parameterized queries,
enhancing performance and security by precompiling SQL
queries and executing stored procedures efficiently.
- PreparedStatement allows for precompiled SQL queries in
JDBC, reducing execution time and preventing SQL injection
attacks for better performance and security in database
interactions.
- CallableStatement is used in JDBC to execute stored
procedures, providing a way to interact with databases
effectively and securely through Java applications.
Best Practices in JDBC
- Implement connection pooling in JDBC to optimize
resource usage, reuse connections, and enhance
application performance and scalability.
- Efficiently manage JDBC connections by handling tasks
like opening, closing, and exception handling for proper
resource utilization and memory leak prevention.
- Follow best practices in JDBC for connection setup,
resource management, and handling to ensure optimal
performance and reliable database interactions.
ADVANCED OBJECT-ORIENTED
PROGRAMMING (223 CCS-3)
CHAPTER – 4
LAMBDA EXPRESSIONS
Prepared By:
Dr. Rincy Merlin Mathew
OUTLINES
Lambda Expressions:
Introducing Lambda Expression
Lambda Expression Fundamentals
Functional Interfaces
Some Lambda Expression Examples
Block Lambda Expressions
Generic Functional Interfaces
Passing Lambda Expressions as Arguments
Lambda Expressions and Exceptions
Predefined Functional Interfaces
Introducing Lambda Expressions
Lambda expressions (and their related features) added by JDK 8, significantly
enhanced Java because of two primary reasons:
First, they added new syntax elements that increased the expressive power of the
language. In the process, they streamlined the way that certain common
constructs are implemented.
Second, the addition of lambda expressions resulted in new capabilities being
incorporated into the API Library.
The addition of lambda expressions also provided the catalyst for other new Java
features, including the default method which lets you define default behavior for
an interface method, and the method reference, which lets you refer to a method
without executing it.
Introducing Lambda Expressions
The key to understanding Java’s implementation of lambda expressions are two constructs.
The first is the lambda expression, itself. The second is the functional interface.
A lambda expression is, essentially, an anonymous (that is, unnamed) method. However,
this method is not executed on its own. Instead, it is used to implement a method defined
by a functional interface. Thus, a lambda expression results in the form of an anonymous
class. Lambda expressions are also commonly referred to as closures.
A functional interface is an interface that contains one and only one abstract method.
Normally, this method specifies the intended purpose of the interface. Thus, a functional
interface typically represents a single action. For example, the standard interface
Runnable is a functional interface because it defines only one method: run( ). Therefore,
run( ) defines the action of Runnable. Furthermore, a functional interface defines the
target type of a lambda expression.
Here is a key point: a lambda expression can be used only in a context in which its target
type is specified. One other thing: a functional interface is sometimes referred to as a SAM
type, where SAM stands for Single Abstract Method.
Lambda Expression Fundamentals
The lambda expression introduced a new syntax element and operator into the Java language. The new
operator, sometimes referred to as the lambda operator or the arrow operator, is −>. It divides a lambda
expression into two parts. The left side specifies any parameters required by the lambda expression. (If
no parameters are needed, an empty parameter list is used.) On the right side is the lambda body, which
specifies the actions of the lambda expression. The −> can be verbalized as “becomes” or “goes to.”
Java defines two types of lambda bodies. One consists of a single expression, and the other type
consists of a block of code.
Lambdas define a single expression.
For example, () -> 123.45. This lambda expression takes no parameters; thus the parameter list is empty. It
returns the constant value 123.45. Therefore, it is similar to the following method: double myMeth() {
return 123.45; } When a lambda expression requires a parameter, it
is specified in the parameter list on the left side of
The method defined by a lambda expression does not have a name. the lambda operator. Here is a simple example: (n)
-> (n % 2)==0 This lambda expression returns
() -> Math.random() * 100 true if the value of parameter n is even.
This lambda expression obtains a pseudo-random value from Math.random( ), multiplies it by 100, and
returns the result. It, too, does not require a parameter.
Functional Interfaces
A functional interface is an interface that specifies only one abstract method.
With JDK 8, it is possible to specify a default implementation for a method declared in an
interface. Private and static interface methods also supply an implementation.
An interface method is abstract only if it does not specify an implementation. Because non-default
non-static, non-private interface methods are implicitly abstract, there is no need to use the
abstract modifier. Here is an example of a functional interface:
In this case, the method getValue( ) is implicitly abstract, and it is the only method defined by
MyNumber. Thus, MyNumber is a functional interface,and its function is defined by getValue( ).
A lambda expression is not executed on its own. Rather, it forms the implementation of the abstract
method defined by the functional interface that specifies its target type. As a result, a lambda
expression can be specified only in a context in which a target type is defined.
Some Lambda Expression Examples
Let’s work through an example that shows how a lambda expression can be used in an assignment context. First, a
reference to the functional interface MyNumber is declared:
// Create a reference to a MyNumber instance.
MyNumber myNum;
Next, a lambda expression is assigned to that interface reference:
// Use a lambda in an assignment context.
myNum = () -> 123.45;
When a lambda expression occurs in a target type context, an instance of a class is automatically created that
implements the functional interface, with the lambda expression defining the behavior of the abstract method
declared by the functional interface.
getValue( ) method
The value 123.45
// Call getValue(), which is implemented by the previously assigned
System.out.println(myNum.getValue()); Because the lambda expression assigned to myNum returns the value 123.45,
that is the value obtained when getValue( ) is called.
Block Lambda Expressions
The body of the lambda consists of a single expression. These types of lambda bodies are
referred to as expression bodies, and lambdas that have expression bodies are sometimes
called expression lambdas. In an expression body, the code on the right side of the lambda
operator must consist of a single expression.
While expression lambdas are quite useful, sometimes the situation will require more than a
single expression. To handle such cases, Java supports a second type of lambda expression
in which the code on the right side of the lambda operator consists of a block of code that
can contain more than one statement. This type of lambda body is called a block body.
Lambdas that have block bodies are sometimes referred to as block lambdas.
A block lambda expands the types of operations that can be handled within a lambda
expression because it allows the body of the lambda to contain multiple statements. For
example, in a block lambda you can declare variables, use loops, specify if and switch
statements, create nested blocks, and so on.
Block Lambda Expressions
A block lambda is easy to create. Simply enclose the body within braces as you would any other block of
statements. When comparing to allowing multiple statements, one key difference is that you must explicitly use
a return statement to return a value. This is necessary because a block lambda body does not represent a single
expression.
Here is an example that uses a block lambda to compute and return the factorial of an int value:
Generic Functional Interfaces
A lambda expression, itself, cannot specify type parameters. Thus, a lambda expression cannot be generic. (Of course,
because of type inference, all lambda expressions exhibit some “generic-like” qualities.) However, the functional interface
associated with a lambda expression can be generic. In this case, the target type of the lambda expression is determined, in
part, by the type argument or arguments specified when a functional interface reference is declared.
Generic Functional Interfaces
Here, T specifies both the return type and the parameter type of func( ). This means that it is compatible with any
lambda expression that takes one parameter and returns a value of the same type.
The SomeFunc interface is used to provide a reference to two different types of lambdas. The first uses type String.
The second uses type Integer.
Thus, the same functional interface can be used to refer to the reverse lambda and the factorial lambda. Only the
type argument passed to SomeFunc differs.
Passing Lambda Expressions as Arguments
Lambda expression can be used in any context that provides a target type. One
of these is when a lambda expression is passed as an argument. In fact, passing
a lambda expression as an argument is a common use of lambdas. Moreover, it
is a very powerful use because it gives you a way to pass executable code as an
argument to a method. This greatly enhances the expressive power of Java.
To pass a lambda expression as an argument, the type of the parameter
receiving the lambda expression argument must be of a functional interface
type compatible with the lambda.
Passing Lambda Expressions as Arguments
Passing Lambda Expressions as Arguments