- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the size of a column of a table using JDBC?
You can get the size of a column of a table using the getPrecision() method of the ResultSetMetaData class.
//Retrieving the ResultSetMetaData object ResultSetMetaData rsmd = rs.getMetaData(); //getting the column type int size_name = rsmd. getPrecision(3);
Assume we have a table named employee_data in the database with description as shown below:
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | Name | varchar(255) | YES | | NULL | | | Dob | date | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+
Following JDBC program establishes connection with the database, retrieves the ResultSetMetaData object of the employee_data table, and prints the size of the third column.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class SizeOfColumn { public static void main(String args[])throws Exception { //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 a Statement object Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee_data"); //Retrieving the ResultSetMetaData object ResultSetMetaData rsmd = rs.getMetaData(); //getting the column type int column_size = rsmd.getPrecision(3); System.out.println("Size of the 3rd column : "+column_size); } }
Output
Connection established...... Size of the 3rd column : 11
- Related Articles
- How to get the datatype of a column of a table using JDBC?
- How to get the number of columns of a table using JDBC?
- How to get the number of records in a table using JDBC?
- How to change the datatype of a column in an existing table using JDBC API?
- How to drop constraint on a column of a table in a database using JDBC API?
- How to get the table name of the current ResultSet using JDBC?
- How to get all the column names from a ResultSet using JDBC
- How to get all table names from a database using JDBC?
- How to get the properties of a driver using JDBC?
- How add a unique key constraint to a column of a table in a database using JDBC API?
- How to add a primary key constraint to a column of a table in a database using JDBC API?
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- How to add a new column to an existing table using JDBC API?
- How to create a table with auto-increment column in MySQL using JDBC?
- How to delete a column from an existing table in a database using JDBC API?

Advertisements