- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java ResultSetMetaData getColumnType() method with example
The getColumnType() method of the ResultSetMetaData (interface) retrieves the type of the specified column in the current ResultSet object.
This method accepts an integer value representing the index of a column and, returns an integer value representing the SQL type of the specified column.
Following is the list of values returned by various datatypes of java.sql.Type −
Array: 2003
Big int: -5
Binary: -2
Bit: -7
Blob: 2004
Boolean: 16
Char: 1
Clob: 2005
Date: 91
Datalink70
Decimal: 3
Distinct: 2001
Double: 8
Float: 6
Integer: 4
JavaObject: 2000
Long var char: -16
Nchar: -15
NClob: 2011
Varchar: 12
VarBinary: -3
Tiny int: -6
Time stamt with time zone: 2014
Timestamp: 93
Time: 92
Struct: 2002
SqlXml: 2009
Smallint: 5
Rowid: -8
Refcursor: 2012
Ref: 2006
Real: 7
Nvarchar: -9
Numeric: 2
Null: 0
Smallint: 5
To get the ResultSetMetaData object, you need to −
Register the Driver: Select the required database register the Driver class of the particular database using the registerDriver() method of the DriverManager class or, the forName() method of the class named Class.
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Get connection: Create a connection object by passing the URL of the database, username and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class.
Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");
Create a Statement object: Create a Statement object using the createStatement method of the connection interface.
Statement stmt = con.createStatement();
Execute the Query: Execute the SELECT query using the executeQuery() methods of the Statement interface and Retrieve the results into the ResultSet object.
String query = "Select * from MyPlayers"; ResultSet rs = stmt.executeQuery(query);
Get the ResultSetMetaData object: Retrieve the ResultSetMetsdata object of the current ResultSet by invoking the getMetaData() method.
ResultSetMetaData resultSetMetaData = rs.getMetaData();
Finally, using the getColumnType() method of the ResultSetMetaData interface get the data type of the specified column as −
int columnType = resultSetMetaData.getColumnType();
Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below -
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
Now, we will insert 7 records in MyPlayers table using INSERT statements −
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following JDBC program establishes connection with MySQL database, retrieves and displays the integer representing the data type of the 4th column in the MyPlayers table using the getColumnType() method.
Example
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 ResultSetMetaData_getColumnType { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Query to retrieve records String query = "Select * from MyPlayers"; //Executing the query ResultSet rs = stmt.executeQuery(query); //retrieving the ResultSetMetaData object ResultSetMetaData resultSetMetaData = rs.getMetaData(); //Retrieving the type of the column (integer value) int columnType = resultSetMetaData.getColumnType(4); System.out.println("Data type of the 4th column of the MyPlayers table: "+ columnType); } }
Output
Connection established...... Data type of the 4th column: 91