CSC419 ยท 2075 ยท 5 marks
How prepared statements are different with statement? List the types of JDBC driver.
How prepared statements are different with statement? List the types of JDBC driver. [5]
Advanced Java ProgrammingChapter 4: Database ConnectivityDDL and DML Operations using Java, Prepared Statements, Mult
Prepared Statements vs Statement & JDBC Driver Types
Part 1: Difference Between PreparedStatement and Statement
| Feature | Statement | PreparedStatement |
|---|---|---|
| Definition | Used to execute simple, static SQL queries | Used to execute precompiled, parameterized SQL queries |
| SQL Compilation | SQL is compiled every time it is executed | SQL is compiled only once and reused |
| Parameters | Does not support parameters; query is hardcoded | Supports ? placeholders for dynamic parameters |
| Performance | Slower for repeated execution | Faster for repeated execution due to precompilation |
| SQL Injection | More vulnerable to SQL injection attacks | More secure; prevents SQL injection |
| Usage | Suitable for queries executed once | Suitable for queries executed multiple times with different values |
Example of Statement:
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE id = 1");
Example of PreparedStatement:
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM users WHERE id = ?");
pstmt.setInt(1, 1);
ResultSet rs = pstmt.executeQuery();
Part 2: Types of JDBC Drivers
There are 4 types of JDBC drivers:
Type 1: JDBC-ODBC Bridge Driver
- Converts JDBC method calls into ODBC method calls.
- Requires an ODBC driver to be installed and configured on the client machine.
- Does not directly interact with the database.
- Can connect to any type of database.
- Least efficient due to multiple layers of conversion.
Type 2: Native-API Driver
- Written partially in Java and partially in native code.
- Converts JDBC method calls into native calls of the database API.
- The Native-API of the specific database must be installed on the client system before use.
- Faster than Type 1 but still requires native installation.
Type 3: Net Pure Java Driver (Network Protocol Driver)
- Fully written in Java.
- The JDBC driver on the client machine uses a socket to communicate with middleware at the server.
- Client database access requests are sent through the network to the middleware, which then communicates with the database.
- No client-side database installation required.
Type 4: Pure Java Driver (Thin Driver)
- Fully written in Java, hence it is a portable driver.
- Interacts directly with the database without any middleware or native library.
- No client-side or server-side installation required.
- Most commonly used driver in modern applications.
Summary: Type 4 (Pure Java Driver) is the most preferred driver due to its portability, direct database interaction, and no dependency on native libraries.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.