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