We can use ResultSetMetaData to get the number of columns, name of the columns, the data type of columns etc that
are returned by executing the select statement. Below is a sample program
package com.ram.app; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class MetaDataExample { public static void main(String[] args) throws Exception{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("dbc:oracle:thin:@localhost:1521:xe","hr","hr"); Statement stmt = conn.createStatement(); String query = "select * from employees"; ResultSet rs = stmt.executeQuery(query); ResultSetMetaData metaData = rs.getMetaData(); System.out.println("Number of columns = "+metaData.getColumnCount()); System.out.println("First column = "+metaData.getColumnName(1)); System.out.println("First column type = "+metaData.getColumnType(1)); } }
No comments:
Post a Comment