Get column's precision and scale value in Java
Description
The following code shows how to get column's precision and scale value.
Example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
// ww w . ja v a 2 s . c o m
public class Main {
public static void main(String[] args) throws Exception {
Connection connection = getConnection();
Statement statement = connection.createStatement();
String query = "SELECT stock_id, name, price FROM stocks";
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);
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}