- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is ResultSetMetaData in JDBC? What is its significance?
The ResultSetMetaData provides information about the obtained ResultSet object like, the number of columns, names of the columns, datatypes of the columns, name of the table etc…
Following are some methods of ResultSetMetaData class.
Method | Description |
---|---|
getColumnCount() | Retrieves the number of columns in the current ResultSet object. |
getColumnLabel() | Retrieves the suggested name of the column for use. |
getColumnName() | Retrieves the name of the column. |
getTableName() | Retrieves the name of the table. |
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class ResultSetMetadataExample { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/TestDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating a Statement object Statement stmt = con.createStatement(); //Retrieving the data ResultSet rs = stmt.executeQuery("select * from Dataset"); ResultSetMetaData rsMetaData = rs.getMetaData(); //Number of columns System.out.println("Number of columns: "+rsMetaData.getColumnCount()); //Column label System.out.println("Column Label: "+rsMetaData.getColumnLabel(1)); //Column name System.out.println("Column Name: "+rsMetaData.getColumnName(1)); //Number of columns System.out.println("Table Name: "+rsMetaData.getTableName(1)); } }
Output
Connection established...... Number of columns: 2 Column Label: mobile_brand Column Name: mobile_brand Table Name: dataset
Advertisements