|
| 1 | + |
| 2 | + |
| 3 | +## Introduction to JDBC |
| 4 | + |
| 5 | +Java Database Connectivity (JDBC) is a Java API for connecting and interacting with relational databases from Java programs. It provides a standard interface for Java applications to access databases, execute SQL queries, and manipulate database data. |
| 6 | + |
| 7 | +### Key Components of JDBC |
| 8 | + |
| 9 | +1. **Driver Manager**: Manages a list of database drivers. It is responsible for loading the appropriate driver based on the database URL provided by the application. |
| 10 | + |
| 11 | +2. **Driver**: Implements the JDBC interfaces to communicate with a specific type of database. Each database vendor provides its own JDBC driver. |
| 12 | + |
| 13 | +3. **Connection**: Represents a connection to a database. It is used to establish communication with the database and provides methods for executing SQL statements. |
| 14 | + |
| 15 | +4. **Statement**: Represents an SQL statement to be executed against the database. It can be a simple statement, a prepared statement, or a callable statement. |
| 16 | + |
| 17 | +5. **ResultSet**: Represents the result of a query executed against the database. It provides methods for navigating through the rows of the result set and retrieving column values. |
| 18 | + |
| 19 | +### Steps to Use JDBC |
| 20 | + |
| 21 | +1. **Load the Driver**: Register the JDBC driver using `Class.forName()` or let the DriverManager automatically load the appropriate driver. |
| 22 | + |
| 23 | +2. **Establish Connection**: Create a connection to the database using `DriverManager.getConnection()` method by providing the database URL, username, and password. |
| 24 | + |
| 25 | +3. **Create Statement**: Create a Statement object using the connection to execute SQL queries. |
| 26 | + |
| 27 | +4. **Execute Query**: Execute SQL queries using the Statement object. For example, `executeQuery()` for SELECT queries and `executeUpdate()` for INSERT, UPDATE, DELETE queries. |
| 28 | + |
| 29 | +5. **Process Results**: Process the results returned by the query using the ResultSet object. |
| 30 | + |
| 31 | +6. **Close Resources**: Close the ResultSet, Statement, and Connection objects when they are no longer needed to release database resources. |
| 32 | + |
| 33 | +### Example Code Snippet |
| 34 | + |
| 35 | +```java |
| 36 | +import java.sql.*; |
| 37 | + |
| 38 | +public class JDBCDemo { |
| 39 | + public static void main(String[] args) { |
| 40 | + try { |
| 41 | + // Load the driver |
| 42 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 43 | + |
| 44 | + // Establish connection |
| 45 | + String url = "jdbc:mysql://localhost:3306/mydatabase"; |
| 46 | + String username = "username"; |
| 47 | + String password = "password"; |
| 48 | + Connection connection = DriverManager.getConnection(url, username, password); |
| 49 | + |
| 50 | + // Create statement |
| 51 | + Statement statement = connection.createStatement(); |
| 52 | + |
| 53 | + // Execute query |
| 54 | + ResultSet resultSet = statement.executeQuery("SELECT * FROM employees"); |
| 55 | + |
| 56 | + // Process results |
| 57 | + while (resultSet.next()) { |
| 58 | + int id = resultSet.getInt("id"); |
| 59 | + String name = resultSet.getString("name"); |
| 60 | + double salary = resultSet.getDouble("salary"); |
| 61 | + System.out.println("ID: " + id + ", Name: " + name + ", Salary: " + salary); |
| 62 | + } |
| 63 | + |
| 64 | + // Close resources |
| 65 | + resultSet.close(); |
| 66 | + statement.close(); |
| 67 | + connection.close(); |
| 68 | + } catch (ClassNotFoundException | SQLException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### JDBC Drivers |
| 76 | + |
| 77 | +There are four types of JDBC drivers: |
| 78 | + |
| 79 | +1. **Type 1 (JDBC-ODBC Bridge)**: Uses ODBC (Open Database Connectivity) to connect to databases. It requires native code and is platform-dependent. |
| 80 | + |
| 81 | +2. **Type 2 (Native-API Driver)**: Uses a database-specific native library to communicate with the database. It is platform-dependent and requires native code. |
| 82 | + |
| 83 | +3. **Type 3 (Network Protocol Driver)**: Communicates with a middle-tier server using a database-independent protocol, which then communicates with the database. It is platform-independent but requires additional software. |
| 84 | + |
| 85 | +4. **Type 4 (Thin Driver, JDBC Net Pure Java Driver)**: Communicates directly with the database using a pure Java implementation. It is platform-independent and does not require additional software. |
| 86 | + |
| 87 | +### Conclusion |
| 88 | + |
| 89 | +JDBC provides a powerful and flexible API for Java applications to interact with relational databases. By following the JDBC architecture and using appropriate drivers, developers can easily connect to databases, execute SQL queries, and manage database data from their Java programs. |
0 commit comments