Skip to content

Commit e123511

Browse files
authored
images
1 parent c91cf96 commit e123511

6 files changed

Lines changed: 72 additions & 3 deletions

File tree

IMAGES/connection.jpg

86.2 KB
Loading

IMAGES/d1.jpg

33.9 KB
Loading

IMAGES/d2.jpg

32.9 KB
Loading

IMAGES/d3.jpg

44.1 KB
Loading

IMAGES/d4.jpg

56.7 KB
Loading

senior-java/advanced-jdbc/index.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ Applications use the JDBC API, which communicates with the JDBC Driver Manager.
104104

105105
### Type-1: JDBC-ODBC Bridge Driver
106106
- **Description:** Converts JDBC calls to ODBC calls to connect to the database.The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. Type-1 driver is also called Universal driver because it can be used to connect to any of the databases.
107+
<br/>
108+
<div align="center" width=700 height=200>
109+
<img src="IMAGES/d1.jpg" class="execution" alt="exception hierarchy">
110+
</div><br/>
111+
107112

108113
- Built into JDK; no separate installation required.
109114
- Database independent.
110115

111116

112117
### Type-2: Native-API Driver
113118
- **Description:** Uses database native client-side libraries to convert JDBC calls.Uses client-side libraries to convert JDBC calls into native database API calls. Requires local API for each database, providing more secure data transfer. Also known as a Partially Java driver.
119+
120+
<br/>
121+
<div align="center" width=700 height=200>
122+
<img src="IMAGES/d2.jpg" class="execution" alt="exception hierarchy">
123+
</div><br/>
114124

115125

116126
- Better performance than Type-1.
@@ -119,31 +129,90 @@ Applications use the JDBC API, which communicates with the JDBC Driver Manager.
119129

120130
### Type-3: Network Protocol Driver
121131
- **Description:** Uses middleware that converts JDBC calls to database-specific protocols.
132+
This driver uses a middleware server to convert JDBC calls into the database-specific protocol. It is more flexible and can be used with multiple databases without needing native libraries.
133+
134+
<br/>
135+
<div align="center" width=700 height=200>
136+
<img src="IMAGES/d3.jpg" class="execution" alt="exception hierarchy">
137+
</div><br/>
138+
122139
- Fully written in Java; portable.
123140
- No client-side libraries required.
124141
- Supports multiple databases and additional middleware features.
125142

126143
### Type-4: Thin Driver
127144
- **Description:** Directly interacts with the database using native protocol; no middleware involved.
145+
This driver converts JDBC calls directly into the database-specific protocol. It is written entirely in Java and does not require any native libraries, making it platform-independent and suitable for web applications.
146+
<br/>
147+
<div align="center" width=700 height=200>
148+
<img src="IMAGES/d4.jpg" class="execution" alt="exception hierarchy">
149+
</div><br/>
128150

129151
- No native library or middleware installation.
130152
- Fully Java-based; portable.
131153

132-
## Choosing the Right Driver
154+
### Choosing the Right Driver
133155

134156
- Use **Type-4** drivers for single database applications (e.g., Oracle).
135157
- Use **Type-3** drivers when accessing multiple different databases simultaneously.
136158
- Use **Type-2** drivers if Type-3 or Type-4 are unavailable for your database.
137159
- **Type-1** drivers are mainly for development and testing, not recommended for production.
160+
### How to Use a JDBC Driver
161+
162+
1. **Add the JDBC Driver to Your Project**:
163+
You can include the JDBC driver in your project by adding the appropriate JAR file to your classpath. For example, for MySQL, you would include `mysql-connector-java-x.x.x.jar`.
164+
165+
2. **Load the Driver**:
166+
You can load the driver using `Class.forName()` method (though this is not always necessary with newer JDBC versions).
138167

139-
## Conclusion
168+
```java
169+
Class.forName("com.mysql.cj.jdbc.Driver");
170+
```
171+
172+
3. **Establish a Connection**:
173+
Use `DriverManager.getConnection()` to establish a connection to the database.
174+
175+
```java
176+
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
177+
```
178+
179+
4. **Create a Statement**:
180+
Create a `Statement` or `PreparedStatement` to execute SQL queries.
181+
182+
```java
183+
Statement statement = connection.createStatement();
184+
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
185+
```
186+
187+
5. **Process the Results**:
188+
Iterate through the `ResultSet` to process the data returned by the query.
189+
190+
6. **Close the Connection**:
191+
Always close the `ResultSet`, `Statement`, and `Connection` objects to free up resources.
192+
193+
```java
194+
resultSet.close();
195+
statement.close();
196+
connection.close();
197+
```
198+
199+
### Conclusion
140200

141201
Each JDBC driver type serves different scenarios with trade-offs in performance, portability, and installation requirements. Choosing the appropriate driver depends on your application’s needs and database environment.
142202

143203

144204
# Java Database Connectivity
145205

146-
During programming, you may need to interact with a database to solve your problem. Java provides JDBC to connect to databases and work with them. Using standard library routines, you can open a connection to the database. JDBC allows the integration of SQL calls into a general programming environment by providing library routines, which interface with the database. In particular, Java’s JDBC has a rich collection of routines which makes such an interface extremely simple and intuitive.
206+
During programming, you may need to interact with a database to solve your problem. Java provides JDBC to connect to databases and work with them. Using standard library routines, you can open a connection to the database.
207+
208+
<br/>
209+
<div align="center" width=700 height=200>
210+
<img src="IMAGES/connection.jpg" class="execution" alt="exception hierarchy">
211+
</div><br/>
212+
213+
214+
215+
JDBC allows the integration of SQL calls into a general programming environment by providing library routines, which interface with the database. In particular, Java’s JDBC has a rich collection of routines which makes such an interface extremely simple and intuitive.
147216

148217
## Steps to Connect to a Database in Java
149218

0 commit comments

Comments
 (0)