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

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

WBOY
Release: 2023-08-26 12:25:12
forward
941 people have browsed it

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));
   }
}
Copy after login

Output

Connection established......
Number of columns: 2
Column Label: mobile_brand
Column Name: mobile_brand
Table Name: dataset
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template