This section contains the detail about the counting Column using metadata in java.
Column count using metadata
In this program ,we will get column count using "resultsetmetadata ".Using resultset interface and metadata interface ,we can get properties of column. The ResultSetMetaData object can be obtained by invoking getMetaData() method on ResultSet object. The function or method ,use for this is "getColumnCount()".we use employee database for this purpose :
import java.sql.*; public class Countcolumn { public static void main(String[] args) throws Exception { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://192.168.10.13:3306/ankdb"; String username = "root"; String password = "root"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); System.out.println("Connected"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee"); ResultSetMetaData rsmd = rs.getMetaData(); int NumOfCol=rsmd.getColumnCount(); System.out.println("Number of Columns="+NumOfCol); st.close(); conn.close(); } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>java Countcolumn Connected Number of Columns=2 |
[ 0 ] Comments