Reading columns of a table

String jdbcDriver = "oracle.jdbc.driver.OracleDriver";
		String dbConnection = "xxx";

		try {
			OracleDataSource oracleDataSource = new OracleDataSource();
			oracleDataSource.setURL(dbConnection);
			connection = oracleDataSource.getConnection();
			String selectQuery = "SELECT * FROM TABLENAME";
			
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery(selectQuery);
			ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
			
			int col = resultSetMetaData.getColumnCount();
			
			for (int i = 1; i <= col; i++){
				  System.out.println( "Column name: "+ resultSetMetaData.getColumnName(i));
				  System.out.println( "Column type: "+ resultSetMetaData.getColumnTypeName(i));
				  }
		
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println(e);
			}
		}

Leave a Reply

Your email address will not be published. Required fields are marked *