Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Get Column Count using ResultSet in SQL

This example counts the number of columns in a table. The ResultSetMetaData interface can be used to get type and properties information of columns in a ResultSet.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

Get Column Count using ResultSet in SQL

  

This example counts the number of columns in a table. The ResultSetMetaData interface can be used to get type and properties information of columns in a ResultSet. The ResultSetMetaData object can be obtained by invoking getMetaData() method on ResultSet object.

In this program you will see how JDBC connection is established and how to get required data, like the number of columns.

GetColumnCount.java

import java.sql.*;
public class GetColumnCount {
  public static void main(String[] args) throws Exception {
  String driver = "com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://localhost:3306/";
  String username = "vineej";
  String password = "vineej";
  String dbName= "no";
  Class.forName(driver);
  Connection conn = DriverManager.getConnection(url+dbName, username, password);
  System.out.println("Connected");
  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery("SELECT * FROM Student");
ResultSetMetaData rsmd = rs.getMetaData();
   int NumOfCol=rsmd.getColumnCount();
System.out.println("Number of Columns="+NumOfCol);
  st.close();
  conn.close();
  }
} 

Java Database Connectivity Steps
1.Connection
An interface in java.sql package that provides connection with the database like- MySQL and java files. The SQL statements are executed within the context of the Connection interface.

2.Class.forName(String driver)
Class.forName method is static. This driver load the class and returns class instance and takes string type value and after that it will match the class with string.

3.DriverManager
It is a class of java.sql package that controls the JDBC drivers. Each driver has to be register with this class.

4.getConnection(String url, String userName, String password)
This method establishes a connection to specified database url. It takes three string types of arguments like:

url: - Database url where stored or created your database
userName: - User name of MySQL
password: -Password of MySQL

5.con.close()
This method is used for disconnecting the connection. It frees all the resources occupied by the database.

6.printStackTrace()
The method is used to show error messages. If the connection is not established then exception is thrown and print the message.

Description of code

1. First import the java packages

import java.sql.*;

The star '*' indicates that all of the classes in the java.sql and java.io packages are to be imported.

2. Loading a database driver
In this step, the driver class loaded by calling Class.forName() with the Driver class. Class is a class in java.lang package.

Class.forName("com.mysql.jdbc.Driver");

3. Creating a jdbc Connection
The objects defined by DriverManager class to establish the applications with the JDBC driver. This class manages the JDBC drivers which is installed on the system. getConnection() is the method by which it can connect. It uses the username, password, and a jdbc url to make the connection with database.

Connection conn = DriverManager.getConnection(url+dbName, username, password);

4. Creating a jdbc Statement object
When an connection is established then it can interact with the database. Connection interface defines methods for managing the database. It used to instantiate a Statement by using the createStatement() method.

Statement st = conn.createStatement();

5. Executing a statement with the Statement object, and returning a jdbc resultSet
This interface defines methods which is used to communicate with database. This class has three methods to execute the statements- executeQuery(), executeUpdate(), and execute(). To SELECT statement, executeQuery() method will be used. For create or modify tables, the method to use is executeUpdate.

ResultSet rs = st.executeQuery("SELECT * FROM Student");

6. Getting ResultSetMetaData object
ResultSet has method
getMetaData() which returns the ResultSet MetaData object which provides meta information of the result set.

ResultSetMetaData rsmd = rs.getMetaData();

7. Getting no of columns
getColumnCount() method on
ResultSetMetaData object returns the number of columns for the result set returned from the query.

int NumOfCol=rsmd.getColumnCount();

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Get Column Count using ResultSet in SQL

Posted on: June 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Get Column Count using ResultSet in SQL   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
ashish
April 13, 2013
java

awesome website. I have a suggestion for the owner of the website that please do something that whenever a user searches anything he/she must be able to see your website name first in Google page.it will help the user to get there answer quickly.

Ads

 

Ads