
Explore JDBC, a vendor-neutral Java API for database access, and learn the four driver types and their bridging, native, middleware, and pure Java approaches.
Learn how JDBC works by using the DriverManager as a factory to obtain a database connection from vendor drivers (Oracle, H2), then create statements and process results.
Set up the H2 database by downloading the h2 jar, running java -jar h2-1.4.197.jar to start a server and browser console, then connect to a test database and create tables.
Set up a Java project in Eclipse and add the H2 database driver. Load the driver class and connect with DriverManager using the connection string, username, and password.
Create a final DBUtil class with a private constructor and static getConnection to load the driver and open a JDBC connection using a properties file.
Create a table from a Java program using a try-with-resources block to manage the JDBC connection and statement, executing a create table command on an H2 database.
Insert a record into the products table from the Java application using JDBC, reading user input via a keyboard utility, and discuss SQL injection risks and shift to prepared statements.
Learn how to insert records in JDBC using PreparedStatement by creating variables, binding parameters with setInt and setString, and executing a precompiled insert on an H2 database.
Learn to insert multiple records with jdbc batch execution by collecting values in a loop and preparing a single statement; then execute the batch once to reduce round trips.
Retrieve multiple records from a database using JDBC by executing a select query with a prepared statement and iterating the result set to print product names and prices.
Learn to fetch a single product record from the database using a prepared statement with a parameter placeholder, safely handling zero or one result.
Learn how to insert into customers with an auto generated primary key using JDBC. Retrieve the generated id via get generated keys after a prepared statement insert.
JDBC (Java DB Connectivity) is a Java Standard API for connecting a Java application to an RDBMS. Although you may not need to use JDBC directly in any application today, it is important to understand how it works. Any modern frameworks like Hibernate or JPA which are ORM (Object-Relation-Mapper) or Mybatis which is an SQL mapper, behind the scene use JDBC.
In this class, we will be looking into the architecture of a JDBC application, connecting to a DB, executing SQL statements, and handling the results of SQL SELECT statements. We will also be learning how to setup a light weight DB called H2, with out having to install anything on our computer.
The class is completely hands-on based, so get ready to code a lot.