You are on page 1of 1

JDBC Java provides a standard API for accessing databases called the Java Database Connectivity (JDBC) API.

Using this , developers can access databases no matter who the vendor may be; the vendors provide the implementations to the abstract interfaces defined in the API, providing the same set of functionality to the developer. JDBC programming interface is found in "java.sql" and "javax.sql" packages. Key classes in the JDBC API: java.sql.Connection represents a connection with the database. Also, it abstracts the details of how to communicate with the database server. java.sql.DriverManager manages JDBC drivers used by the application. In conjunction with the proper driver URL and proper authentication, it can provide applications with valid instances of Connection objects. javax.sql.DataSource abstracts the details (URL, authentication details) of how to obtain a connection to the database. It is newer and more preferred method of obtaining Connection objects. java.sql.Statement provides methods for the developer to execute SQL statements. java.sql.ResultSet represents the results of an SQL statement. These objects are usually returned from methods in the Statement object. java.sql.DriverManager Using this class, a developer can retrieve a Connection object which he then can use to perform database activities. There are two steps required: First, the JDBC driver must first be registered with the DriverManager. This can be done by using the Class.forName method to load the driver's class definition into memory. Second, use the getConnection method in the DriverManager supplying a JDBC URL, as well as the username and password authenticated for database access. The URL must follow the syntax required by the particular database implementation. javax.sql.DataSource DataSource is an interface defined in the JDBC API since version 2 of its specification. Nowadays, it is also the recommended way for a developer to get a Connection object. Retrieval of the Connection object is very straightforward: simply call the getConnection() method in a valid instance of DataSource. It is obtaining an instance of DataSource that can now pose a problem for some developers. java.sql.Connection / java.sql.Statement The java.sql.Connection objects represent actual connections to the database. Once we have an instance of this object, we can create an instance of a Statement object, which we can then use to perform SQL queries. The Statement object provides a number of methods to execute SQL queries. The two most used are: executeQuery takes in SELECT statements and returns the result of the operation as a ResultSet object. executeUpdate takes in INSERT, UPDATE, or DELETE statements and returns the number of rows affected as an integer primitive. java.sql.ResultSet A ResultSet object encapsulates the results of a query to the database. The data inside a ResultSet object can best be visualized as a table. The information can then be retrieved one row at a time, with the ResultSet object keeping track of which row is current.

You might also like