JDBC (Java Database Connectivity) is used to connect database to perform database operations: create, read, update, delete.
1What is Java JDBC ?
Java JDBC is an API to connect and execute the query with the database. JDBC API uses JDBC drivers to connect with the database.
2. What are the different types of statements in JDBC?
JDBC API has 3 Interfaces and every interface has it’s own features. key interfaces are:
Statement: which is used to run simple SQL statements like select and update. Statement interfaces used for general-purpose to access database. It is useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters, it accept query only. if you have any parameter, you can pass by concatenation of string, but this is not preferable due to SQL injection.
PreparedStatement: A SQL statement is pre-compiled and stored in a Prepared Statement object. It is used to run Precompiled SQL. This is used to execute statements multiple times with parameters or without parameters. The object of Prepared Statement class can be created using
Connection.prepareStatement() method. PreparedStatement extends Statement interface.
CallableStatement: This interface is used to execute the stored procedures. This extends PreparedStatement interface. The object of Callable Statement class can be created using
4. Why we use JDBC ?
Before JDBC, ODBC API was the database API to connect and execute queries with the database. But, ODBC API uses an ODBC driver which is written in C language (i.e. platform-dependent, slow and unsecured). That is why, Java has defined it’s own API (JDBC API) that uses JDBC drivers (written in Java language).
5. What are different types of JDBC drivers?
There are four types of drivers defined by JDBC as follows:
- JDBC-ODBC driver: these require an ODBC (Open Database Connectivity) driver for the database to be installed. It is used for local connection.
- Native API (partly-Java driver): This type of driver uses a database API to interact with the database. It also provides no host redirection.
- Network Protocol Driver: It makes use of a middle-tier between the calling program and the database. The client driver communicates with the net server using a database-independent protocol and the net server translates this protocol into database calls.
- Native Protocol Drive: there is no middle-tier between this driver and database. This is direct call to databse from your java code. This has a same configuration as a type 3 driver but uses a wire protocol specific to a particular vendor and hence this can access only specified database.
Happy learning 🙂