
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Count the number of columns in a MySQL table with Java
For this, use ResultSetMetaData. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentFirstName varchar(20), -> StudentLastName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
The Java code is as follows −
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.ResultSetMetaData; public class ResultSetDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; Statement st = null; ResultSet rs = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root", "123456"); String query = "select StudentId,StudentFirstName,StudentLastName from DemoTable"; st = con.createStatement(); rs = st.executeQuery(query); ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData(); int numberOfColumn = rsmd.getColumnCount(); System.out.println(" Number Of Columns: " + numberOfColumn); System.out.println("All Details Of Columns:"); for (int i = 1; i <= numberOfColumn; i++) { String columnName = rsmd.getColumnName(i); String dataTypeOfColumn = rsmd.getColumnTypeName(i); System.out.println(columnName + " has data type " + dataTypeOfColumn); } } catch (Exception e) { e.printStackTrace(); } } }
output
Number Of Columns: 3 All Details Of Columns: StudentId has data type INT StudentFirstName has data type VARCHAR StudentLastName has data type VARCHAR
The snapshot of the output is as follows −
- Related Articles
- How to count number of columns in a table with jQuery
- Get the number of columns in a MySQL table?
- Find the count of EMPTY or NULL columns in a MySQL table?
- How to select the table with the greatest number of columns in MySQL?
- How to find the number of columns in a MySQL table?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- Count number of rows in each table in MySQL?
- Counting the number of non-null or nonzero columns in a MySQL table?
- MySQL multiple COUNT with multiple columns?
- How to count the number of columns having specific value in MySQL?
- How to Count the Number of Rows in a MySQL Table in Python?
- How to know the exact number of table and columns in a MySQL database?
- Fastest way to count number of rows in MySQL table?
- MySQL query to count number of duplicate values in a table column
- What are the restrictions, in terms of a number of rows and columns, with MySQL query having no table list?

Advertisements