Java ResultSetMetaData getScale() method with example


The getScale() method of the ResultSetMetaData (interface) retrieves the number of digits after the right of the decimal point in the given column.

This method accepts an integer value representing the index of a column. and returns an integer value representing the number of digits after the decimal in the specified column.

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, user-name 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 dispatches";
ResultSet rs = stmt.executeQuery(query);

Get the ResultSetMetsdata object: Retrieve the ResultSetMetsdata object of the current ResultSet by invoking the getMetaData() method.

ResultSetMetaData resultSetMetaData = rs.getMetaData();

Finally, using the getScale() method of the ResultSetMetaData interface get scale of a particular column as −

int columnCount = resultSetMetaData.getScale();

Let us create a table with name dispatches in MySQL database using CREATE statement as follows −

CREATE TABLE dispatches(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate Date,
   DeliveryTime Time,
   Price INT,
   Location VARCHAR(255),
   DispatchTimeStamp timestamp
);

Now, we will insert 5 records in dispatches_data table using INSERT statements −

insert into dispatches values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into dispatches values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into dispatches values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into dispatches values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into dispatches values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

Following JDBC program establishes connection with MySQL database, retrieves and displays the scale of the 5th column of the dispatches table using the getScale() 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_getScale {
   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 dispatches";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      //retrieving the ResultSetMetaData object
      ResultSetMetaData resultSetMetaData = rs.getMetaData();
      //Retrieving the scale of a column
      int columnScale = resultSetMetaData.getScale(5);
      System.out.println("Scale of the 5th column of the Dispatches table: "+ columnScale);
   }
}

Output

Connection established......
Scale of the 5th column of the Dispatches table: 3

Updated on: 30-Jul-2019

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements