Skip to main content

Java Database Connectivity

Relational Database Basicsโ€‹

Schemaโ€‹

A schema represents a logical grouping, or namespace, for database objects such as tables, views, and procedures. In some database systems, like PostgreSQL and Oracle, the term schema is used to
represent the namespace within a database where objects are organized. These systems can have multiple schemas within a single database. In other database systems, like MySQL, the term schema is often used interchangeably with database.

Common Database Objectsโ€‹

ObjectDescription
tableA table stores rows or records of data, in attribute fields, with values specific to that record.
indexAn index consists of a table name, a key value and a record locator field, to quickly access a record from a table. A primary key is a unique identifier for a record.
viewA view is a stored query, which can be accessed like a table, but hides the details of the table implementation from the client.
userA user represents a package a privileges to database artifacts given to an account.

SQLโ€‹

In databases, the language lets us create objects, populate them with information, create relationships, and query data. This language is called the Structured Query Language, or SQL.

DDLโ€‹

The Data Definition Language is used to define, create, manage, and modify the database objects. DDL statements don't manipulate the data in the object, instead they manipulate data structures, that store and organize the data.

CommandDescription
createUsed to create database objects like tables, indexes, views, and schemas.
alterUsed to modify the structure of existing database objects.
dropUsed to delete or remove database object.
truncateUsed to remove all rows from a table while keeping the table structure intact.
renameUsed to rename database objects.

DMLโ€‹

The Data Manipulation Language, is used to interact with, and manipulate, the data stored within the database objects or artifacts. DML statements perform operations like inserting, updating, retrieving, and deleting data in the database.

CommandDescription
selectUsed to retrieve data from one or more tables.
insertUsed to add new rows of data into a table.
updateUsed to modify existing data in a table.
deleteUsed to remove rows from a table.

Relationsโ€‹

Database tables can be associated with one another, through different kinds of relationships.

RelationshipDescription
One to OneOne row in the first table is related to only one row in a second table.
One to ManyOne row in the first table is related to many rows in a second table.
Many to ManyMany rows in the first table are related to many rows in a second table.

Normalization & Joinโ€‹

On normalization and normal forms check out this. A join is a SQL clause, that combines rows from two or more tables, based on a common field.

Join TypeResult
inner joinReturns all rows from both tables where the join condition is met
left joinReturns all rows from the left table, even if there is no matching row in the right table.
right joinReturns all rows from the right table, even if there is no matching row in the left table.
full joinReturns all rows from both tables, regardless of whether there is a matching row in the other table.
cross joinReturns all possible combinations of rows from the two tables.

JDBCโ€‹

It abstracts the complexities of connecting to different databases, through a common interface.

Driverโ€‹

To use a particular data source from an application, we need a JDBC driver for that data source. A driver is simply a Java library, containing classes that implement the JDBC API.

At the most basic level, drivers allow us to:

  • Connect to the database. Each database may have a different mechanism to establish a connection to it.
  • Execute SQL statements. These statements can be DML statements, any of the CRUD statements for example, or they can be DDL statements.
  • Execute Stored procedures. This sends a request to the database to execute a procedure or function stored in the database.
  • Retrieve and process results. This could be a set of data from a select statement, or a count of rows updated or inserted.
  • Handling Database Exceptions.

java.sql and javax.sqlโ€‹

JDBC consists of two packages, java.sql which is core JDBC, and javax.sql, which provides the API for server side data source access.

Purposejava.sqljavax.sql
Makes a connection with a driverDriverManagerDataSource
Query ResultsResultSetRowSet

DriverManager should be replaced with DataSource in most cases, because it's newer and supports a lot more functionality. For querying results, there's the standard ResultSet type, but the RowSet interface provides many advantages.