IBM Support

Java™ API sample code snippet

How To


Summary

This sample code snippet provides a sample code snippet that lists all available metadata in the DVM server.

Objective

This example programmer code in Figure 1 is representative of Output for the included reusable code snippet.
Code snippet sample
Figure 1.  Sample output for the included reusable code snippet
This example output in Figure 2 is representative of Output that includes all available columns for two Virtual Tables over VSAM (STAFFVS) and zFS (SYSLOGD) data sources.
Sample output of java code snippet with all columns
Figure 2.  Sample output of the Java™ code snippet which lists all available columns
Reusable code snippet for listing metadata:
(import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class DVMMetadataTablesApp {
	protected Properties props = new Properties(); 
	protected Connection conn;
	protected Statement stmt;
	protected ResultSet rs;
	private long start;
	private DateFormat df = new SimpleDateFormat("dd.MM.yyyy  
 HH:mm:ss:SSSSSSS");
	private long previousTime = start;
	protected Connection getConnection() 
throws Exception {
String url = "jdbc:rs:dv:DatabaseType=DVS; Host=XXX; Port=YYY;";
	props.setProperty("user", "AAA");
	props.setProperty("password", "BBB");

show("----------");
		show("Connecting using url ");
		show("url = " + url);
	  	conn = DriverManager.getConnection (url, props);
	  	show("Connected !");
	  	show("----------");
	  	conn.setAutoCommit(false);
	  	return conn;
	}
public static void main(String[] args) throws Exception 
{
DVMMetadataTablesApp dvmApp = new
DVMMetadataTablesApp();
dvmApp.getConnection();
	for(int i=1; i <= 1; i++) {
		dvmApp.show("Run " + i);
		dvmApp.fetchMetadata();
		dvmApp.show("");
}
		dvmApp.closeAll();
	}
	
	private void show(Object msg) 
{
		System.out.println(msg);
	}
	
private void fetchMetadata() throws Exception 
{
	// TODO Auto-generated method stub
	start = System.currentTimeMillis();
	log("Fetch Metadata - START", false);
	DatabaseMetaData databaseMetadata = conn.getMetaData();
	log("Conn.getMetaData(): " + databaseMetadata, true);
	printWithCRLF("databaseMetadata=" + databaseMetadata);
	String catalog = conn.getCatalog();
	String schemaPattern = null;
	String tableNamePattern = "%";
	String[] types = null;
printWithCRLF("catalog: " + catalog + ", schemaPattern: " + schemaPattern + ", tableNamePattern: "+ tableNamePattern + ", types: "+ types );
ResultSet tables = databaseMetadata.getTables(catalog, schemaPattern, tableNamePattern, types);
	printWithCRLF("tables=" + tables);
	int columnCount = printHeader(tables);
	int rowCounter = 0;
	while(tables.next()) 
{
	for(int i=1; i <= columnCount; i++) 
{
	String value = tables.getString(i);
	if(value == null && i == 1) 
{
	value = " " + ++rowCounter + " ";
	}
	printWithOUTCRLF(value + " |\t");
	}
	printWithCRLF("");
	}
log("Processed DatabaseMetaData.getTables().getColumns(). Tables:" + rowCounter + " Columns: " + columnCount, true);
	long end = System.currentTimeMillis();
	long elapsed = end - start;
log("Fetch Metadata - END. START-to-END took total " + elapsed + " ms (" + (elapsed/1000) + " secs).", false);
	closeStatement();
}
private int printHeader(ResultSet tables) throws SQLException 
{
	ResultSetMetaData resultSetMetaData = tables.getMetaData();
	int columnCount = resultSetMetaData.getColumnCount();
	printWithCRLF("columnCount=" + columnCount);
printWithCRLF("--------------------------------------------------");
	int lineLengh = 0;
	for(int i=1; i <= columnCount; i++) {
printWithOUTCRLF(resultSetMetaData.getColumnName(i) + " | ");
	lineLengh += resultSetMetaData.getColumnDisplaySize(i);
	}
	printWithCRLF("");
	for(int i=1; i <= lineLengh; i++) 
{
	printWithOUTCRLF("-");
	}
	printWithCRLF("");
	return columnCount;
	}
	
private void printWithCRLF(String message) 
{
		System.out.println(message);
	}

	private void printWithOUTCRLF(String message) 
{
		System.out.print(message);
	}

	private void log(String message, boolean showInterimTime) 
{
		long currentTime = System.currentTimeMillis();
		String interimTime = "";
		if(showInterimTime) 
{
interimTime = " (" + ( currentTime - previousTime) + 
"ms)";
		}
show(df.format(new Date()) + "\t" + message + interimTime);
		previousTime = currentTime;
	}
	
	protected void closeStatement() throws Exception 
{
		if(rs != null) 
{try
{
		rs.close();
		} catch (Exception e) 
{
			//ignore
		   }
		}
		
if(stmt != null) 
{try
{
		stmt.close();
		} catch (Exception e) 
{
			//ignore
		   }
		}
	}
	
protected void closeAll() throws Exception 
{
		closeStatement();
conn.commit();
		ifconn != null;
{try}

Document Location

Worldwide

[{"Type":"SW","Line of Business":{"code":"LOB10","label":"Data and AI"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SS4NKG","label":"IBM Data Virtualization Manager for z\/OS"},"ARM Category":[{"code":"a8m0z000000cxATAAY","label":"Studio"}],"ARM Case Number":"","Platform":[{"code":"PF035","label":"z\/OS"}],"Version":"All Version(s)"}]

Product Synonym

DVM

Document Information

Modified date:
24 June 2021

UID

ibm16447770