Java ResultSetMetaData getPercision() method with example



In this article, we will learn how to retrieve the size of a specified column in a ResultSet object using the getPrecision() method of the ResultSetMetaData interface in JDBC.

What does getPrecision() in ResultSetMetaData do?

The getPercision() method of the ResultSetMetaData (interface) retrieves the size 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 size of the given column.

Steps to get the ResultSetMetaData object

To get the ResultSetMetaData object, you need to ?

  • Step 1. Register the Driver: Select the required database and 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());
  • Step 2. 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");
  • Step 3. Create a Statement object: Create a Statement object using the createStatement method of the connection interface.
Statement stmt = con.createStatement();
  • Step 4. Execute the Query: Execute the SELECT query using the executeQuery() method of the Statement interface and Retrieve the results into the ResultSet object.
String query = "Select * from MyPlayers";
ResultSet rs = stmt.executeQuery(query);
  • Step 6. Get the ResultSetMetsdata object: Retrieve the ResultSetMetsdata object of the current ResultSet by invoking the getMetaData() method.
ResultSetMetaData resultSetMetaData = rs.getMetaData();
  • Step 7. Using the getPrecision() method of the ResultSetMetaData interface get the size of the required column as ?
int columnSize = resultSetMetaData.getPrecision();
  • Step 8. Let us create a table with the name MyPlayers in the MySQL database using the 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)
);
  • Step 9. We will insert 7 records in the 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');

Java example of ResultSetMetaData getPercision() method

Following JDBC program establishes a connection with MySQL database, retrieves and displays the size of the 4th column of the MyPlayers table using the getPrecision() method ?

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_getPercision {
	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 size of the specified column
		int columnCount = resultSetMetaData.getPrecision(4);
		System.out.println("Size of the 4th column of the Myplayers table: "+ columnCount);
	}
}

Output

Connection established......
Size of the 4th column of the Myplayers table: 10
Updated on: 2025-02-10T11:28:01+05:30

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements