This example show how we can use the ResultSetMetadata
class to get the number of columns and column names of the selected table. The ResultSetMetaData
class can also be used to get the column type and its properties.
Using the ResultSetMetaData
class might help you to create an inquiry program where you don’t have all information about a table columns.
package org.kodejava.jdbc;
import java.sql.*;
import java.util.ArrayList;
public class MetadataColumnExample {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) throws Exception {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
// In the statement below we'll select all records from users
// table and then try to find all the columns it has.
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT * FROM books");
// The ResultSetMetaData is where all metadata related
// information for a result set is stored.
ResultSetMetaData metadata = resultSet.getMetaData();
int columnCount = metadata.getColumnCount();
// To get the column names we do a loop for a number of column
// count returned above. And please remember a JDBC operation
// is 1-indexed so every index begin from 1 not 0 as in array.
ArrayList<String> columns = new ArrayList<>();
for (int i = 1; i < columnCount; i++) {
String columnName = metadata.getColumnName(i);
columns.add(columnName);
}
// Later we use the collected column names to get the value of
// the column itself.
while (resultSet.next()) {
for (String columnName : columns) {
String value = resultSet.getString(columnName);
System.out.println(columnName + " = " + value);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Some result generated by our above examples are:
isbn = 9781617291999
title = Java 8 in Action
published_year = 2015
Maven dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
Latest posts by Wayan (see all)
- How do I split large excel file into multiple smaller files? - April 15, 2023
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023