For instance, you have a number or double typed column in a database table, and you want to get the precision and the scale of that column. Here is an example for you.
package org.kodejava.jdbc;
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 ScaleAndPrecisionExample {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
Statement statement = connection.createStatement();
String query = "SELECT id, quantity, price FROM purchase_order_detail";
ResultSet resultSet = statement.executeQuery(query);
ResultSetMetaData metadata = resultSet.getMetaData();
int precision = metadata.getPrecision(3);
int scale = metadata.getScale(3);
System.out.println("Precision: " + precision);
System.out.println("Scale : " + scale);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
The result:
Precision: 10
Scale : 2
Maven dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
Latest posts by Wayan (see all)
- How do I split large excel file into multiple smaller files? - April 15, 2023
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023