How to Use Methods of Column to Count Number of Columns in JDBC?
Last Updated :
07 Jan, 2021
A single standalone program cannot be able to fulfill all the demands of the clients every request of the client has some different requests like the update, delete, insertJDBC is one of the famous API of the java language that is used to connect relational databases working on the Structured Query Language with the java programs.JDBC along with the database driver is capable of accessing databases and spreadsheets. The enterprise data stored in a relational database(RDB) can be accessed with the help of JDBC APIs.
The main tasks of the JDBC API
- Establish a connection with a database
- Send SQL statements to the database server.
- Process the result obtained.
Illustration: Suppose MySQL Client
Input :
- Database : GFG
- Table : Aayush
Output :

Approach: Steps for connectivity between Java program and database
- Importing the database files.
- Load and register drivers to java program.
- Establish the connections.
- Create a statement.
- Execute the query.
- Process the results.
- Close the connections.
Now, discussing the above steps in order figuring out how to use methods of the column to count no of columns in JDBC
1. Loading and registering drivers: To begin with, you first need to load the driver or register it before using it in the program. Registration is to be done once in your program. You can register a driver in one of the two ways is by using Class.forName() method. It is used to load the driver’s class file into memory at the runtime and there is no need of using new or creation of an object. In order to illustrate for Oracle driver it is as follows:
Class.forName(“oracle.jdbc.driver.OracleDriver”);
2. Establish the connections: After loading the driver, establish connections by creating an object of Connection class of main class as depicted:
Connection con = DriverManager.getConnection(url,user,password)
- user: username from which your SQL command prompt can be accessed.
- password: password from which your SQL command prompt can be accessed.
- con: Connection class object is a reference to the Connection interface.
- URL: Uniform Resource Locator.
If Oracle is used as a database then it is treated in a similar manner but with a little gimmick to it as follows.
- The driver used @localhost is the IP address where the database is stored.
- 1521 is the port number.
- The service provider is ‘xe’ here.
All 3 parameters here are of String type and are to be declared by the programmer before calling the function and using this can be referred from the final code. It can be created as follows:
String url = “ jdbc:oracle:thin:@localhost:1521:xe”
OR
String mysqlUrl = "jdbc:mysql://localhost/Databasename";
3. Create a statement: Once a connection is established you can interact with the database. The JDBCStatement, Callable Statement and PreparedStatement interfaces define the methods that enable you to send SQL commands and receive data from your database. Use of JDBC Statement is as follows. Here, the con is a reference to the Connection interface used in the previous step.
Statement st = con.createStatement();
4. Execute the query: Now comes the most important part i.e executing the query. The query here is an SQL Query. Now we know we can have multiple types of queries. Some of them are as follows:
- The query for updating/inserting table in a database.
- The query for retrieving data.
The executeQuery() method of Statement interface is used to execute queries of retrieving values from the database. This method returns the object of ResultSet that can be used to get all the records of a table. The executeUpdate(sql query) method of the Statement interface is used to execute queries of updating/inserting.
Example
Java
import java.util.*;
import java.sql.*;
public class GFG {
public static void main(String args[])
throws ClassNotFoundException, SQLException
{
DriverManager.registerDriver(
new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection(
mysqlUrl, "root" , "Aayush" );
System.out.println( "Connection established......" );
Statement stmt = con.createStatement();
ResultSet rs
= stmt.executeQuery( "select * from aayush;" );
ResultSetMetaData rsmd = rs.getMetaData();
int column_count = rsmd.getColumnCount();
System.out.println(
"Number of columns in the table : "
+ column_count);
}
}
|
Output:

Same table is used in the implementation part. So, the above output is of corresponding table generated below on which count is called.

Similar Reads
Java Program to Use Methods of Column to Get Column Name in JDBC
Java Database Connectivity or JDBC is a Java API (application programming interface) used to perform database operations through Java application. It allows the execution of SQL commands for the creation of tables and data manipulation through Java application. Java supports java.sql package which c
4 min read
How to Use Different Row Methods to Get Number of Rows in a Table in JDBC?
Java supports many databases and for each database, we need to have their respective jar files to be placed in the build path to proceed for JDBC connectivity. For different databases, different jar files are imported to make a connection given below or their built path is supposed to be added for s
5 min read
How to Update Contents of a Table using JDBC Connection?
JDBC (Java Database Connectivity) is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend( for storing data). Steps to Update the conte
3 min read
How to Sort Contents of a Table in JDBC?
Sorting the contents of a table means rearranging the records in an organized way to make data more usable. You can sort all the records of a table by choosing a column within a table according to which data has to be sorted. In simple words, when you sort data, you arrange the data in a logical ord
4 min read
How to Add a New Column to a Table Using JDBC API?
Java has its own API which JDBC API which uses JDBC drivers for database connections. JDBC API provides the applications-to-JDBC connection and JDBC driver provides a manager-to-driver connection. Following are the 5 important steps to connect the java application to our database using JDBC. Registe
2 min read
How to Execute a SQL Query with Named Parameters in JDBC?
Executing the SQL (Structured Query Language) query with named parameters in JDBC (Java Database Connectivity) is the fundamental feature of the database interaction in Java. Named parameters provide convenient and it provides a secure way to pass the values into the SQL queries without using concat
4 min read
How to Execute a SQL Query with Pagination in JDBC?
To execute a SQL query with pagination in JDBC (Java Database Connectivity), normally we use SQL syntax to limit the number of rows returned and then iterate through the results as per the need. We often use SQL's LIMIT and OFFSET clauses to execute page SQL queries in JDBC. Steps to Execute SQL Que
3 min read
How to Create a Statement Object in JDBC ?
JDBC stands for Java Database Connectivity. It is used in Java-based API which allows Java Applications to interact and perform operations on relational databases. Creating a "Statement" object in JDBC is an important step while dealing with a database in Java. A "Statement" object is used to execut
4 min read
Java Program to Delete a Column in a Table Using JDBC
Before deleting a column in a table, first is need to connect the java application to the database. Java has its own API which JDBC API which uses JDBC drivers for database connections. Before JDBC, ODBC API was used but it was written in C which means it was platform-dependent. JDBC API provides th
3 min read
How to Execute a SQL Query Using JDBC?
Java programming provides a lot of packages for solving problems in our case we need to execute SQL queries by using JDBC. We can execute a SQL query in two different approaches by using PreparedStatement and Statement. These two interfaces are available in java.sql package. When comparing both of t
4 min read