Java Database Connectivity (JDBC)

Last Edited

by

in

Definition of Java Database Connectivity (JDBC) in The Network Encyclopedia.

What is Java Database Connectivity?

Java Database Connectivity (JDBC) is a standard Structured Query Language (SQL) data access interface developed by Sun Microsystems that allows Java applications to access databases. Java Database Connectivity (JDBC) is based on open database connectivity (ODBC) and is used with the Java programming language.

Java Database Connectivity - JDBC
Java Database Connectivity

The JDBC application programming interface (API) is a standard component of Java 2.0 Platform.



How it works

The JDBC API specifies a set of Java classes that represent database connections, SQL queries and their result sets, and other objects associated with accessing databases. Multiple drivers for JDBC exist that allow access to different database formats, and the JDBC drivers themselves can be implemented either within applets or as native methods on the operating system.

JDBC also supports drivers that act as a bridge between ODBC and JDBC. This type of driver translates JDBC function calls into native ODBC calls, but this bridge cannot be run by untrusted applets within a Web browser environment.

Java Database Connectivity example

When a Java application needs a database connection, one of the DriverManager.getConnection() methods is used to create a JDBC connection. The URL used is dependent upon the particular database and JDBC driver. It will always begin with the “jdbc:” protocol, but the rest is up to the particular vendor.

Connection conn = DriverManager.getConnection(
     "jdbc:somejdbcvendor:other data needed by some jdbc vendor",
     "myLogin",
     "myPassword" );
try {
     /* you use the connection here */
} finally {
    //It's important to close the connection when you are done with it
    try { conn.close(); } catch (Throwable e) { /* Propagate the original exception
instead of this one that you want just logged */ logger.warn("Could not close JDBC Connection",e); }
}

Starting from Java SE 7 you can use Java’s try-with-resources statement to make the above code cleaner:

try (Connection conn = DriverManager.getConnection(
     "jdbc:somejdbcvendor:other data needed by some jdbc vendor",
     "myLogin",
     "myPassword" ) ) {
     /* you use the connection here */
}  // the VM will take care of closing the connection

Once a connection is established, a statement can be created.

try (Statement stmt = conn.createStatement()) {
    stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
}

Note that Connections, Statements, and ResultSets often tie up operating system resources such as sockets or file descriptors. In the case of Connections to remote database servers, further resources are tied up on the server, e.g., cursors for currently open ResultSets. It is vital to close() any JDBC object as soon as it has played its part; garbage collection should not be relied upon. The above try-with-resources construct is a code pattern that obviates this.



Data is retrieved from the database using a database query mechanism. The example below shows creating a statement and executing a query.

try (Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery( "SELECT * FROM MyTable" )
) {
    while ( rs.next() ) {
        int numColumns = rs.getMetaData().getColumnCount();
        for ( int i = 1 ; i <= numColumns ; i++ ) {
           // Column numbers start at 1.
           // Also there are many methods on the result set to return
           //  the column as a particular type. Refer to the Sun documentation
           //  for the list of valid conversions.
           System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
        }
    }
}

See also:

Search