
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 getScale() method with example
In this article, we will learn the ResultSetMetaData getScale() method in Java. The ResultSetMetaData interface provides metadata about the columns in a ResultSet object. The getScale() method returns the number of digits to the right of the decimal point for a specified column.
What is the getScale() method?
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.
Syntax
The following are the parameters of the getScale() method ?
-
column: The column index (starting from 1) for which the scale is required.
-
Returns: The number of digits to the right of the decimal point for the specified column.
- Throws SQLException: If the column index is invalid or a database access error occurs.
Using getScale() in a MySQL Database
The following program demonstrates how to use the getScale() method. It connects to a MySQL database, retrieves retrieves, and displays the scale of the specified column.
To get the ResultSetMetaData object, you need to ?
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());
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 the scale of a particular column as ?
int columnCount = resultSetMetaData.getScale();
Let us create a table with name dispatches in MySQL database using the 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 the 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');
Example
Following JDBC program establishes a connection with MySQL database, retrieves and displays the scale of the 5th column of the dispatches table using the getScale() 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_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
Conclusion
The getScale() method in ResultSetMetaData is a powerful tool to retrieve the decimal precision of numeric columns dynamically. This ensures proper handling of data in Java applications interacting with databases.