How do I get column's precision and scale value?
Category: java.sql, viewed: 593 time(s).
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.example.sql; 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 DRIVER = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost/testdb"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) throws Exception { Connection connection = null; try { Class.forName(DRIVER); connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); 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); } catch (SQLException e) { e.printStackTrace(); } finally { connection.close(); } } } |
The result:
Precision: 10 Scale : 2
Related Examples
- How do I make updates in Updatable ResultSet?
- How do I create a batch update in JDBC?
- How do I create a scrollable result sets?
- How do I move to absolute or relative row?
- How do I know the current position of cursor?
- How do I check if cursor is in the last row?
- How do I check if cursor is in the first row?
- How do I move cursor to the last record?
- How do I move cursor in scrollable result sets?
- How do I use DatabaseMetaData to get table column names?
|
|