JDBC
JDBC
JDBC
1.
2.
3. JDBC Architecture:
Java Application: A java programs that runs stand alone in a client or server.
JDBC Driver Manager: This class manages lists of database drivers. It ensures that
correct driver is used to access each data source.
JDBC Driver: This interface handles the communications with the database.
Statement: The Statement interface is used for executing simple SQL queries. It allows
you to execute SQL statements and retrieve results. There are three types of
statements in JDBC: Statement, PreparedStatement, and CallableStatement.
ResultSet: The ResultSet interface represents the result set of a SQL query. It provides
methods for accessing and processing the query results.
A.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
// JDBC objects
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 1. Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Establish a database connection
connection = DriverManager.getConnection(jdbcURL, username, password);
B.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
// Create a statement
Statement statement = connection.createStatement();
// Close resources
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java 8 introduced several significant features, with functional interfaces and lambda
expressions being among the most notable ones. These features enable a more
functional programming style in Java, making it easier to write cleaner and more
concise code. Let's dive into each of these features:
Functional Interfaces:
A functional interface is an interface that contains only one abstract method. Java 8
introduced the @FunctionalInterface annotation to indicate that an interface is a
functional interface. Functional interfaces are a crucial concept for using lambda
expressions effectively.
java
@FunctionalInterface
interface MyFunctionalInterface {
void myMethod();
}
Lambda Expressions:
Lambda expressions are a concise way to define anonymous functions (or lambda
functions) in Java. They allow you to treat functionality as a method argument or
code as data. Lambda expressions are particularly useful when working with
functional interfaces.
The syntax for a lambda expression is as follows:
or
(parameter list) -> expression
or
For example, let's create a Predicate using a lambda expression to check if a number
is even:
java
Predicate<Integer> isEven = (number) -> number % 2 == 0;
Method References:
java
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(System.out::println);
Java 8 also introduced the ability to define default and static methods within
interfaces. Default methods provide a default implementation for interface methods,
allowing backward compatibility with existing classes. Static methods in interfaces are
used for utility methods related to the interface.
java
interface MyInterface {
void abstractMethod();
Java 8's functional interfaces, lambda expressions, and method references make it
easier to write more expressive and concise code, particularly when working with
collections, streams, and other functional programming paradigms. These features
enable a more versatile and modern programming style in Java.