Chapter 13. PostgreSQL JDBC

Java Database Connectivity (JDBC) is an API that a user allows to connect to a database from within a Java application in a standardized way. The API is independent of a specific database vendor, and allows creation of applications that can easily be adapted for use with different databases.

This chapter describes the way to use JDBC for connecting to a database, and using a PostgreSQL database from within a Java application.

After discussing how to install and initialize the PostgreSQL JDBC driver and opening a connection to a database, the fundamental concepts and features of the JDBC API will be explained.

This chapter covers executing the basic SQL statements introduced in Chapter 5, SQL Language, from within Java code and handling the results returned from a query.

Later in this chapter, the reader will learn how to call stored procedures, and how to access their results.

For this chapter, it is assumed that the reader has a basic knowledge of Java and the usage of one of the common IDEs like Eclipse or Netbeans.

Introduction to JDBC

JDBC is a database-independent application programming interface (API) for enabling the interaction between Java applications and a database. It was specified by the Oracle Corporation, and was first released with Sun JDK 1.1 in 1997.

JDBC does not implement any database-specific instructions by itself, but provides a common framework of interfaces against which the database vendors can implement a driver for a specific database. This enables a Java developer to write code that can be used to connect to any database that provides a JDBC driver without changing the implementation.

JDBC can be divided into four core components:

  • JDBC driver: A collection of Java database-specific Java classes that implement the interfaces defined in JDBC
  • Connections: Each connection object holds one connection to a database, and is responsible for all the communication with the database
  • Statements: Statements and their subtypes are used to execute queries and updates on a database
  • ResultSets: Queries on a database return a ResultSet that holds the returned data of a query and meta information about the queried table
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset