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.
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:
ResultSet
that holds the returned data of a query and meta information about the queried table18.217.15.45