You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: senior-java/advanced-jdbc/index.md
+72-3Lines changed: 72 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,13 +104,23 @@ Applications use the JDBC API, which communicates with the JDBC Driver Manager.
104
104
105
105
### Type-1: JDBC-ODBC Bridge Driver
106
106
-**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.
- Built into JDK; no separate installation required.
109
114
- Database independent.
110
115
111
116
112
117
### Type-2: Native-API Driver
113
118
-**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,31 +129,90 @@ Applications use the JDBC API, which communicates with the JDBC Driver Manager.
119
129
120
130
### Type-3: Network Protocol Driver
121
131
-**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.
- Supports multiple databases and additional middleware features.
125
142
126
143
### Type-4: Thin Driver
127
144
-**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.
- Use **Type-4** drivers for single database applications (e.g., Oracle).
135
157
- Use **Type-3** drivers when accessing multiple different databases simultaneously.
136
158
- Use **Type-2** drivers if Type-3 or Type-4 are unavailable for your database.
137
159
-**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).
138
167
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.
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
140
200
141
201
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.
142
202
143
203
144
204
# Java Database Connectivity
145
205
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.
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.
0 commit comments