Home  >  Article  >  Database  >  What is ResultSetMetaData in JDBC? What's the point?

What is ResultSetMetaData in JDBC? What's the point?

WBOY
WBOYforward
2023-08-26 12:25:12871browse

JDBC 中的 ResultSetMetaData 是什么?其意义何在?

ResultSetMetaData Provides information about the obtained ResultSet object, such as column number, column name, column data type, table name, etc...

The following are some methods of the ResultSetMetaData class.

Retrieve the number of columns in the current ResultSet object. tr>Retrieve the suggested name of the columnRetrieve the name of the column. Retrieve the name of the table.
Method Description
##getColumnCount()
getColumnLabel()
getColumnName()
getTableName()
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

The above is the detailed content of What is ResultSetMetaData in JDBC? What's the point?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete