ResultSetMetaData in JDBC example
The object of java.sql.ResultSetMetaData can be used to get the details of the columns available in the ResultSet object.
Some of the important methods of ResultSetMetaData are
1 2 3 4 5 6 | 1. getColumnCount() 2. getColumnDisplaySize(int column) 3. getColumnLabel(int column) 4. getColumnName(int column) 5. getColumnType(int column) 6. getColumnTypeName(int column) |
Lets see ResultSetMetaData in JDBC example using some of the above methods
ResultSetMetaData in JDBC example
Details of the Database table
1 2 3 4 5 6 7 | CREATE TABLE employee_details( id int(5) not null, first_name varchar(255), last_name varchar(255), designation varchar(255), salary int(5) ) |
Now we will write a sample code that will get the details of each of the columns using ResultSetMetaData
Please Note : Column number starts from 1. So when you use getColumnName(int column) or any other method, the parameter should start from 1 (and not 0). See the for loop in the sample code starts with int i = 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.kscodes.sampleproject.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class ResultSetMetaDataExample { public static String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; public static String CONNECTION_URL = "jdbc:mysql://localhost:3306/mysql"; public static String CONNECTION_USER = "kscodes"; public static String CONNECTION_PASSWORD = "kscodes"; public static void main(String[] args) { String selectSql = "SELECT * FROM employee_details"; try (Connection connection = getConnection(); Statement statement = connection.createStatement()) { ResultSet rs = statement.executeQuery(selectSql); ResultSetMetaData resultSetMetaData = rs.getMetaData(); int columnCount = resultSetMetaData.getColumnCount(); System.out.println("Total Number of Columns are :: " + columnCount); for (int i = 1; i <= columnCount; i++) { String columnName = resultSetMetaData.getColumnName(i); int columnType = resultSetMetaData.getColumnType(i); String columnTypeName = resultSetMetaData.getColumnTypeName(i); System.out.println("Column Name ::" + columnName); System.out.println("Column Type ::" + columnType); System.out.println("Column Type Name ::" + columnTypeName); System.out.println("-------------------------------"); } } catch (SQLException e) { System.out.println("An exception occured while Selecting records from Table. Exception is :: " + e); } } public static Connection getConnection() { try { Class.forName(DRIVER_CLASS_NAME); } catch (ClassNotFoundException e) { System.out.println("Error while registering JDBC driver"); return null; } Connection connection = null; try { connection = DriverManager.getConnection(CONNECTION_URL, CONNECTION_USER, CONNECTION_PASSWORD); } catch (SQLException e) { System.out.println("Failed to create Connection"); return null; } System.out.println("Connection created Successfully."); return connection; } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Connection created Successfully. Total Number of Columns are :: 5 Column Name ::id Column Type ::4 Column Type Name ::INT ------------------------------- Column Name ::first_name Column Type ::12 Column Type Name ::VARCHAR ------------------------------- Column Name ::last_name Column Type ::12 Column Type Name ::VARCHAR ------------------------------- Column Name ::designation Column Type ::12 Column Type Name ::VARCHAR ------------------------------- Column Name ::salary Column Type ::4 Column Type Name ::INT ------------------------------- |