Before going for table data retrieval, check the whole process of table data getting added and fetched. Any program is executed with 3 basic processes. If we think about the program, it is actually a block of code to be executed for operating. Hence, to perform an operation we require a few of the things, known as the input, and achieve the required result known as an output.
Three processes are tiers of program execution. The first tier is a user interface, where we can take data as input. The second tier is business logic, which is our programming language. Technically UI provides data as requirements of the client or executer from the program. To fulfill the requirement developer will develop the code. To develop the code, they use different programming languages, certainly like Java. To fulfill the operations, it may need to access some of the stored data. And this stored data is known as database which is the third tier.
Accessing the database is one of the basic tasks for every language. Java language takes the help of Java DataBase Connectivity (JDBC). Database stores data in the form of tables and records. JDBC allows to access the database and performs CRUD operations on it. Using JDBC, we create a bridge between Java code and database. Exchange of data like inserting, updating, deleting, and retrieval we execute SQL Query. JDBC has classes for it. ExecuteUpdate method from class Statement helps to execute insert, update, and delete commands on data from a table. The important task is to check the available data in tables and provide those to the user. For this purpose, the ResultSet class is used.
Four Steps to Retrieve Table Data
- Import Library:
In the market, we can find many different databases. Each database platform provides library files to access that database from the respective language code. To perform database operations, the very first thing we need to do is to add those jar files of the library into our project.
The above mysql.jar file should be added to the project. This will result in an added reference library in project.
- Load the drivers:
To perform any db operations, we need to load the drivers. Driver class helps to load the driver to establish the connection. The path of the driver class is passed to the forName() method of the Class class.
- Establish Connection:
Loaded class has permitted to access db. In the next step, the whole path of the database (URL) to be used, username, and password of the database has been provided. Henceforth, the DriverManager class which is handling the task of connecting the db with the current project, calls the getConnection() method to retrieve the connection.
- Retrieval of table data:
As there is data inside the table,
A query will be created to fetch the data from the database. Statement class wraps the query. Using this class method select query is going to be executed. After retrieving the data, all the data is temporarily stored in the ResultSet class’s object.
This object is the same as the table in the database. While unwrapping the table data, we need to take care regarding column sequence, name, and data type. According to these columns, every row is fetched. One by one all records(row) are unwrapped and displayed to the user. ResultSet class contains different methods to get data of different data types.
For example, if the table contains varchar data, then we can use the getString() method. If the table has int data, then use the get () method from the ResultSet class.
How to retrieve the ResultSet object?
Statement class contains methods to execute the query like executeUpdate() and executeQuery(). Query statements like update, insert and delete, executeUpdate() method used. For every type of retrieval executeQuery() method is called. ResultSet object is the result of the above method. All wrapped data is temporarily stored in this object.
As discussed earlier, the table contains records in the row format. Every row contains all this column information like roll number, name, and marks. Display this data using methods of the ResultSet class.
Methods From ResultSet:
As mentioned earlier, fetched data is wrapped in the ResultSet object. To access those data, methods are provided. Using these methods we can retrieve required data of any data type. Methods are available for each and every data type. To read the boolean value, get Boolean (). To read float values, getFloat(). etc.
Retrieval is a key aspect of CRUD operations. In JDBC, ResultSet is the backbone of fetching data. It itself full of methods to unwrap the data and make it available for users to enhance the programming experience.
reference link: https://en.wikipedia.org/wiki/Result_set