Write access, principles

Connecting an application to a database by establishing a connection between client and database server:

Figure 837. Networking between clients and database server Slide presentation
Networking between clients and database server

Figure 838. JDBC features Slide presentation
  • Protocol connecting database client and server.

  • Vendor dependent implementations.

So JDBC™ is just one among a whole bunch of protocol implementations connecting database servers and applications. Consequently JDBC™ is expected to appear in the lower layer of multi-tier applications. We take a three-tier application as a starting point:

Figure 839. JDBC™ in a three-tier application Slide presentation
JDBC™ in a three-tier application

We may add an additional layer. Web applications are typically being build on top of an application server (WebSphere, Glassfish, Jboss,...) providing additional services:

Figure 840. JDBC™ connecting application server and database. Slide presentation
JDBC™ connecting application server and database.

Opening a connection to a database server requires:

Figure 841. JDBC connection parameter Slide presentation
  1. Database server type i.e. Oracle, DB2, Informix, Postgresql, Mysql etc. due to vendor specific JDBC™ protocol implementations.

  2. Server DNS name or IP number.

  3. Server service's port number.

  4. The database name within the given server.

  5. Optional: A database user's account name and password.

Items 1 - 4 will be encapsulated into a so called JDBCURL. We consider a typical example corresponding to the previous parameter list:

Figure 842. Components of a JDBC™ URL Slide presentation
Components of a JDBC™ URL

Figure 843. IETF Uniform Resource Identifier Slide presentation


absoluteURI   = scheme ":" ( hier_part | opaque_part )

hier_part     = ( net_path | abs_path ) [ "?" query ]

net_path      = "//" authority [ abs_path ]

abs_path      = "/"  path_segments

Figure 844. URL examples Slide presentation
  • http://www.hdm-stuttgart.de/aaa

  • http://someserver.com:8080/someResource

    Non-standard port 8080

  • ftp://mirror.mi.hdm-stuttgart.de/Firmen

Figure 845. Sub protocol examples Slide presentation
Database JDBC URI
PostgreSQL jdbc:postgresql://<HOST>:<PORT>/[database]
MySQL jdbc:mysql://[host][:port]/[database][?p1=v1]...
Oracle jdbc:oracle:thin:[user/password]@[host][:port]:SID
DB2 jdbc:db2://<HOST>:<PORT>/[database]
Derby jdbc:derby://[host][:port]/[database]
MS. SQL S. jdbc:sqlserver://host[:port];user=xxx;password=xyz
Sybase jdbc:sybase:Tds:<HOST>:<PORT>/[database]

Figure 846. No standard port assignments ... Slide presentation






  • No official IETF standard port assignments

  • Vendor specific defaults

  • Explicit port specification required

Figure 847. ... but Postgresql made it into Linux Slide presentation
>grep postgresql /etc/services 
postgresql      5432/tcp       postgres      # PostgreSQL Database
postgresql      5432/udp       postgres

Writing JDBC™ based applications follows a simple scheme:

Figure 848. JDBC architecture Slide presentation
JDBC™ architecture

From a programmer's point of view the java.sql.DriverManager is a bootstrapping object: Other objects like Connection instances are being created from this central and unique object.

Figure 849. DriverManager: Bootstrapping connections Slide presentation

Figure 850. Example: Mysql connection implementation Slide presentation

Figure 851. Driver libraries Slide presentation
  • postgresql-42.1.4.jar

  • mysql-connector-java-x.y.z.jar

  • ojdbc6.jar

Figure 852. Driver libraries by Maven Slide presentation
  • <groupId>postgresql</groupId>
  • <groupId>com.oracle</groupId>  <!-- requires access credentials -->

Figure 853. Driver unavailable Slide presentation
  • conn = DriverManager.getConnection(
     "jdbc:postgresqll://localhost/hdm", "hdmuser", "XYZ");
  • java.sql.SQLException: No suitable driver found for
      at java.sql.DriverManager.getConnection(DriverManager.java:689)
      at java.sql.DriverManager.getConnection(DriverManager.java:247)
      at de.hdm_stuttgart.mi.sda1.DatabaseTest.initDatabase(DatabaseTest.java:34)

Figure 848, “JDBC™ architecture ” does not show details about the relations between java.sql.Connection, java.sql.Statement and java.sql.ResultSet objects. We start by giving a rough description of these three interfaces' tasks and responsibilities:

Figure 854. Connection interface Slide presentation

Figure 855. Statement interface Slide presentation

Two distinct operation classes:


INSERT, UPDATE, DELETE: Integer return code


SELECT: Returning java.sql.ResultSet, see the section called “Read Access”.

Figure 856. JDBC™ instances and relationships. Slide presentation
JDBC™ instances and relationships.

Figure 858. Important Statement methods Slide presentation

Figure 859. JDBC and threading. Slide presentation

From JDBC and Multithreading:

Because all Oracle JDBC API methods are synchronized, if two threads try to use the connection object simultaneously, then one will be forced to wait until the other one finishes its use.


Figure 860. JDBC connection pooling Slide presentation
try (final Connection conn =
       C3P0DataSource.getInstance().getConnection()) {

  final PreparedStatement pstmt = conn.create...;
  // Auto close connection, back to pool.
} catch (SQLException e) {