In this post, we will talk and learn about ResultSetMetaData in the JDBC example
The ResultSetMetaData is a very important interface in java.sql package in JDBC API It is used to get the metadata about a java.sql.ResultSet object. Whenever we query the database using the SELECT SQL statement, the result is stored in a ResultSet object. The ResultSet object is associated with one java.sql.ResultSetMetaData object. This object is having all the metadata about a ResultSet object like schema name, table name, number of columns, column name, the data type of a column, etc. We usually get this ResultSetMetaData object using getMetaData() method of ResultSet interface.
How To Get ResultSetMetaData Object?
We have getMetaData() method in java.sql.ResultSet interface that returns ResultSetMetaData object associated with a ResultSet object. Using the below syntax to get the ResultSetMetaData object.
1 |
ResultSetMetaData metaData = resultSet.getMetaData(); |
ResultSetMetaData in the JDBC example:
SQL Query to create employee_table and insert few rows in MySQL (jdbcdb schema)
(Note:Make sure you have created employee_table and inserted few records in jdbcdb database schema)
DB.sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CREATE TABLE `employee_table` ( `employee_id` int(11) NOT NULL auto_increment, `employee_name` varchar(60) NOT NULL, `email` varchar(45) NOT NULL, `salary` double default NULL, `date_of_joining` datetime default NULL, `bonus` decimal(10,0) default NULL, PRIMARY KEY (`employee_id`) ) ; INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) |
After running above db script in your MySQL jdbcdb schema employee_table will have following content.
DBUtil.java class which is responsible to connect with MySQL database.
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 |
package com.infotech.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /* * @author KK JavaTutorials * Utility class to provide database connection */ public class DBUtil { private static final String DB_DRIVER_CLASS="com.mysql.jdbc.Driver"; private static final String DB_USERNAME="root"; private static final String DB_PASSWORD="root"; private static final String DB_URL ="jdbc:mysql://localhost:3306/jdbcdb"; private static Connection connection = null; static{ try { Class.forName(DB_DRIVER_CLASS); connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } public static Connection getConnection(){ return connection; } } |
Now let’s run our ClientTest.java class to show some important methods of ResultSetMetaData interface
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 |
package com.infotech.client; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import com.infotech.util.DBUtil; /** * @author KK JavaTutorials *JDBC program to show some important methods of ResultSetMetaData */ public class ClientTest { public static void main(String[] args) throws SQLException { String SQL = "SELECT *FROM employee_table"; try (Connection connection = DBUtil.getConnection(); PreparedStatement ps = connection.prepareStatement(SQL,ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);ResultSet rs = ps.executeQuery()) { //Retrieves the number, types and properties of this ResultSet object's columns. ResultSetMetaData metaData = rs.getMetaData(); //Returns the number of columns in this ResultSet object. int columnCount = metaData.getColumnCount(); System.out.println("Total no. of columns:"+columnCount); //Get the designated column's name and column's database-specific type name. for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnName(i); String columnTypeName = metaData.getColumnTypeName(i); System.out.println(columnName+" is a type of "+columnTypeName); } } catch (Exception e) { e.printStackTrace(); } } } |
After running ClientTest.java program you will look below output on eclipse console:
1 2 3 4 5 6 7 |
Total no. of columns:6 employee_id is a type of INT employee_name is a type of VARCHAR email is a type of VARCHAR salary is a type of DOUBLE date_of_joining is a type of DATETIME bonus is a type of DECIMAL |
That’s all about ResultSetMetaData in JDBC example
You May Also Like:
How to store file in MySQL database using JDBC
How to read files/CLOB data from MySQL database using JDBC
Writing and Reading MySQL BLOB/binary files Using JDBC
How to get primary key value (auto-generated keys) from inserted queries using JDBC?
If you have any feedback or suggestion please feel free to drop in blow comment box.