Advanced Java Programming - Old Questions
8. How prepared statements are different with statement? List the types of JDBC driver.[2+3]
Difference between Statement and Prepared Statement
Statement |
PreparedStatement |
It
is used when SQL query is to be executed only once. |
It
is used when SQL query is to be executed multiple times. |
We cannot pass parameters
at runtime. |
We can pass parameters at
runtime. |
Used
for CREATE, ALTER, DROP statements. |
Used
for the queries which are to be executed multiple times. |
Performance is very low. |
Performance is better
than Statement. |
Used to execute normal
SQL queries. |
Used to execute dynamic
SQL queries. |
JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:
Type 1: JDBC-ODBC Bridge
- To use a Type 1 driver in a client machine, an ODBC driver should be installed and configured correctly.
- This type of driver does not directly interact with the database. To interact with database, it needs ODBC driver.
- The JDBC-ODBC bridge driver converts JDBC method class into ODBC method calls.
- It can be used to connect to any type of the databases.
Type 2: Native –API driver
- Type 2 drivers are written partially in Java and partially in native code.
- The Native-API of each database should be installed in the client system before accessing a particular database. So in this way, a Native-API driver is database specific driver.
- This driver converts JDBC method calls into native calls of the database API.
Type 3: Net Pure Java Driver
- In a Type 3 driver, the JDBC driver on the client machine uses the socket to communicate with the middleware at the server. The middleware or server acts as a gateway to access the database.
- The client database access requests are sent through the network to the middleware or server and then the middleware converts the request to the database specific API.
- Type-3 drivers are fully written in Java, hence they are portable drivers.
Type 4: Pure Java Driver
- This driver interact directly with database. It does not require any native database library and middleware server, that is why it is also known as Thin Driver.
- No client-side or server-side installation.
- It is fully written in Java language, hence they are portable drivers.