What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.
- Making a connection to a database.
- Creating SQL or MySQL statements.
- Executing SQL or MySQL queries in the database.
- Viewing & Modifying the resulting records.
Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for portable access to an underlying database. Java can be used to write different types of executables, such as −
- Java Applications
- Java Applets
- Java Servlets
- Java ServerPages (JSPs)
- Enterprise JavaBeans (EJBs).
All of these different executables are able to use a JDBC driver to access a database, and take advantage of the stored data.
JDBC provides the same capabilities as ODBC, allowing Java programs to contain database-independent code.
Pre-Requisite
Before moving further, you need to have a good understanding of the following two subjects −
JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for database access but in general, JDBC Architecture consists of two layers −
- JDBC API: This provides the application-to-JDBC Manager connection.
- JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.
The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases.
Example code :
package com.uprr.lup.event.loup_integration.check_otm_availability;
import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.math.BigInteger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.util.ReflectionTestUtils; import com.uprr.lup.event.config.MainConfig; import com.uprr.lup.event.loup_integration.check_otm_availability.dao.ConnectivityTestDAO; import com.uprr.lup.event.loup_integration.check_otm_availability.dao.ProcessingStateDataDAO; import com.uprr.lup.event.loup_integration.check_otm_availability.dto.ConnectivityTestDTO; import com.uprr.lup.event.loup_integration.check_otm_availability.dto.TimedJobTriggered;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MainConfig.class, })
public class EventHandlerTest {
@Autowired
private EventHandler eventHandler;
@Test
public void testHandle_Success() throws Exception {
MulesoftClient mulesoftClient = mock(MulesoftClient.class);
when(mulesoftClient.testOTMAvailability()).thenReturn(“AVAILABLE”);
ReflectionTestUtils.setField(eventHandler, “mulesoftClient”, mulesoftClient);
ConnectivityTestDAO connectivityTestDAO = mock(ConnectivityTestDAO.class);
when(connectivityTestDAO.insert(any(ConnectivityTestDTO.class))).thenReturn(1);
ReflectionTestUtils.setField(eventHandler, “connectivityTestDAO”, connectivityTestDAO);
ProcessingStateDataDAO processingStateDataDAO = mock(ProcessingStateDataDAO.class);
when(processingStateDataDAO.updateStatus(any(String.class), any(String.class))).thenReturn(1);
ReflectionTestUtils.setField(eventHandler, “processingStateDataDAO”, processingStateDataDAO);
TimedJobTriggered timedJobTriggered = new TimedJobTriggered(“LoupInt”, “update-customer-commitment-to-otm-timed-job”, BigInteger.ONE);
eventHandler.handle(timedJobTriggered);
verify(mulesoftClient, times(1)).testOTMAvailability();
verify(connectivityTestDAO, times(1)).insert(any(ConnectivityTestDTO.class));
}
}
Following is the architectural diagram, which shows the location of the driver manager with respect