JDBC or Java Database Connectivity is an API for client-side access to a database. The database is a relational database.
When you're dealing with exceptions in a JDBC world, all JDBC operations throw a SQL exception or some subclass of it. You have to catch it or throw it.
Terminology of JDBC
1. DriverManager - a class that interacts with the driver for creating connections
DriverManager should operate with everthing that we need in order to get simple connections.
2. DataSource - a modern class that interacts with the driver for creating connections
These DataSource classes provide a modern way of interacting with the driver itself, providing things like thread pooling and other metrics that make connecting to the database more efficient.
3. Connection - a class that the developer interacts with that manages the actual communication between the client and the server
4. Statement - the representation SQL to be excuted against the database
Statement can either be a query or an actual SQL statement that is going to modify the database.
5. ResultSet - the response from the database in a logical "tabular" form
Spin through that ResultSet to pull data out, and also to manipulate the data as we get it out.
6. PreparedStatement - an extension of statement that is used for precompiled statements(with inputs)
7. CallableStatement - an extension of PreparedStatement that references a stored procedure in the database (with inputs and outputs)
Basic Process of JDBC
Transactions
A transaction allows you to have a block of code that all executes together with a single commit.
1. Auto-commit: a function of the database driver where each statement is immediately readable by all processes once executed in the RDBMS.
2.Transaction: a series of statements that must be executed completely or not at all before other processes can read them.
3. Rollback: a mechanism by which all of the statements of a transaction are removed from the database such that it would appear to all current and future processes as this had never occurred.
JDBC vs ORM vs JPA
Why Use JDBC?
- Technology is all about trade-offs.
- Object relational mapping can cause just as many problems as it solves.
- JDBC gives you control, but with a lot of manual work.
- JDBC gives your database administrators more control, which can improve performance.
ORM Obejct Relational Mapping
- Allows you to interact with databases through objects
- Known as a technique, but most developers call the libary an ORM
- Tends to reduce code, especiallly in result set mapping
- Hibernate is a common implementation of a libary that uses ORM
JPA Java Persisttence API
- Standard Java EE(Jakarta EE) specification for ORM
- Hibernate is an implementation of JPA
- Streamlines performance to standard format
- Reduces JDBC code
- Focus on OOP