JDBC Technology, Implicit Objects, and Data Flows in Java

JDBC Technology

JDBC (Java Database Connectivity) technology allows programs from Java or JSP to connect to a database and execute SQL statements for queries. This technology is detailed at length on the website: http://java.sun.com/jdbc/.

The JDBC API

The JDBC API is a standard technology created by Sun. Its use allows developers of Java and JSP pages to access a database engine regardless of its type.

The most important features of the JDBC API are:

  • Possibility of using the SQL (Structured Query Language)
  • Easy access to all features of SQL
  • The results returned by SQL can be treated as objects so that any errors can be treated as exceptions

Sun’s JDBC API is inspired by the philosophy of the ODBC (Open Database Connectivity) standard created by Microsoft. This philosophy is based on creating a standard for using any database with the Java language. That’s why Sun created a sort of gateway or bridge between JDBC and ODBC. This bridge is responsible for converting JDBC calls to ODBC in a way that is completely transparent to the programmer. The JDBC-ODBC bridge is not the most adequate solution because the conversion process from one API to another causes poor performance in an application when using queries in a massive database.

Types of JDBC Drivers

There are four different types of drivers or bridges in the JDBC specification defined by Sun:

  • Bridge 1: Drivers that use the system as a gateway or bridge. One example is JDBC-ODBC. This is not the best solution because, in many cases, it is necessary to install specific software on the client, and it is slow to access the database.
  • Bridge 2: This type of driver is called a native API. The driver contains Java code that makes calls to the native methods of the database in C or C++. Sometimes, it is necessary to install client software to use these types of drivers.
  • Bridge 3: JDBC drivers of this type are reported with an intermediate application server using sockets that move client program requests to a specific API driver. This type of driver has the advantage of not requiring any software on the client.
  • Bridge 4: Drivers of this type use network protocols included in the DBMS (Database Management System), and therefore, the drivers communicate directly with the database, also using Java sockets. It is the best because these drivers are written entirely in Java. Most of these drivers are provided by the manufacturer of your DBMS.

Implicit Objects in JSP

There are some implicit or embedded objects that are available at all times to the developer of JSP pages. There is no need to instantiate them, as they are available in the JSP container. These objects exist to simplify the programming of JSPs.

Page Object

It is an instance of the class java.lang.Object, and its scope is of type page. It represents an instance of a class generated from a JSP page, i.e., the current JSP page. When you want to refer to this JSP page, use the word this.

Config Object

It is an instance of the javax.servlet.ServletConfig class and handles everything related to the configuration of the servlet generated by requesting the JSP page. Its scope of operation is of type page.

Request Object

It is an instance of javax.servlet.ServletRequest and contains information about data sent to the server via a web page. For example, the getParameter() method of the request object contains values sent through a form or a URL. Thus, we can recover the value or contents of the fields in a form on a website. Example:

  <% 
    String name = request.getParameter("name"); 
  %>

Session Object

It is an instance of the class javax.servlet.http.HttpSession. The role of this object is to manage all activities related to the user session; therefore, the area of use is of type session. A session is automatically created (unless otherwise specified) when a user requests a JSP page, and we can store information about that user.

For example, if we want to store an object in the session and later recover it:

  <% 
    HttpSession unaSesion = request.getSession(); 
    unaSesion.setAttribute("user", "pancho_lopez"); 
  %>

Application Object

It is an instance of the class java.servlet.ServletContext, and its area of use is of type application. It represents the web application in which the JSP is running.

Data Flows in Java

The simplest model of an algorithm consists of three connections: data input, processing, and data output. Input/output (I/O) is a fundamental aspect of computing. The computer would not be very useful if it could not receive data from the outside world and present the computed data.

What is a Flow?

A stream is a communication system implemented in the java.io package designed to store and retrieve information in each of the various storage devices.

We can imagine a stream as a pipe where we can read or write bytes. We do not care what may be at the other end of the tube: it may be a keyboard, a monitor, a file, a process, a TCP/IP connection, or a Java object.

All flows that appear in Java, included within the package java.io, belong to two common abstract classes: java.io.InputStream for input flows (those from which we read) and java.io.OutputStream for output flows (those in which we can write).

These flows can have different origins (a file, a TCP socket, etc.), but once you have a reference to them, we always work the same way: reading data using the methods of the family read() or writing data with the methods write().