![]() |
In Java database connectivity, the ResultSet is an interface, and it has a lot of built-in methods for handling the results of SQL queries. In this article, we will learn to retrieve data from a ResultSet in JDBC. ResultSet in InterfaceResultSet interface represents the result set of a database query by using SQL.
Now we will explore this concept to understand this concept in a better way. In this article, we learn about ResultSet in JDBC and Its functionality and how to retrieve data from a ResultSet in JDBC. Syntax:The below statements is representing the syntax of ResultSet with PreparedStatement object. PreparedStatement preparedStatement = connection.prepareStatement(sql_query); We can observe syntax of ResultSet, which gets the ResultSet by using preparedStatement. This preparedStatement is used for executing the SQL queries. ResultSet in JDBCThe ResultSet is an interface available in JDBC which is used for data handling by using result object of preparedStatement class. Implementation of ResultSet
Implementation to retrieve data from a ResultSet in JDBCBelow we have provided one example for ResultSet interface and Its functionality. For this example,
In this example, by using ResultSet we will see the data which is already exist in books table.
Data in Book Table:Below we can see the existing Table and Its data. Below is the example to retrieve data from a ResultSet in JDBC. Java
Output:Here we have the output of the available data. Explanation of the code:In this above code, first we have created connection for MySQL database by using Connection class which is available in java.sql package. While connecting we need to provide some properties like Database URL, Username, Password. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
After this connection we write SQL query for selecting all records from the table by using PreparedStatement class with connection object. Refer below query statement for better understanding. String selectQuery = "SELECT * FROM book"; Now we got result of SQL query. So, now we call the executeQuery method by using statement object. Now we assign this result to ResultSet after that by using resultSet.next() in while we display the result in the console. (Already provided the output image above) ResultSet resultSet = statement.executeQuery(); |
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |