How do you extract the metadata from a database you're connecting to? This answer and more in part two of our JDBC series.
This tutorial is part two of the JDBC-Revisited tutorial series. In the last part, we demonstrated how easy it is to connect to a data source regardless of whether it's a Relational or Big Data or SaaS source, and how to execute simple queries using Progress DataDirect JDBC drivers.
In this tutorial, we will be focusing on how to extract the metadata of the data source to which you are connecting. For those who don’t know what metadata is, it characterizes your data and makes easier for anyone to understand and consume it. To put it simply, metadata is the data describing the data that is being stored in your data source.
Metadata generally includes the name, size and number of rows of each table present in a database, along with the columns in each table, their data types, precisions, etc. With this in mind, let’s start with the tutorial and learn how you can fetch metadata from any data source.
databaseMetaData = connection.getMetaData();
//Print TABLE_TYPE "TABLE"
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});
System.out.println("Printing TABLE_TYPE \"TABLE\" ");
System.out.println("----------------------------------");
while(resultSet.next())
{
//Print
System.out.println(resultSet.getString("TABLE_NAME"));
}
resultSet = databaseMetaData.getTables(null, null, null, new String[]{"SYSTEM TABLE"});
System.out.println("Printing TABLE_TYPE \"SYSTEM TABLE\" ");
System.out.println("----------------------------------");
while(resultSet.next())
{
//Print
System.out.println(resultSet.getString("TABLE_NAME"));
ResultSet columns = databaseMetaData.getColumns(null,null, tableName, null);
while(columns.next())
{
String columnName = columns.getString("COLUMN_NAME");
String datatype = columns.getString("DATA_TYPE");
String columnsize = columns.getString("COLUMN_SIZE");
String decimaldigits = columns.getString("DECIMAL_DIGITS");
String isNullable = columns.getString("IS_NULLABLE");
String is_autoIncrment = columns.getString("IS_AUTOINCREMENT");
//Printing results
System.out.println(columnName + "---" + datatype + "---" + columnsize + "---" + decimaldigits + "---" + isNullable + "---" + is_autoIncrment);
}
//GetPrimarykeys
ResultSet PK = databaseMetaData.getPrimaryKeys(null,null, tableName);
System.out.println("------------PRIMARY KEYS-------------");
while(PK.next())
{
System.out.println(PK.getString("COLUMN_NAME") + "===" + PK.getString("PK_NAME"));
}
//Get Foreign Keys
ResultSet FK = databaseMetaData.getImportedKeys(null, null, tableName);
System.out.println("------------FOREIGN KEYS-------------");
while(FK.next())
{
System.out.println(FK.getString("PKTABLE_NAME") + "---" + FK.getString("PKCOLUMN_NAME") + "===" + FK.getString("FKTABLE_NAME") + "---" + FK.getString("FKCOLUMN_NAME"));
}
We hope this tutorial gave you a basic idea on how you can access the metadata of your data source using Progress DataDirect JDBC drivers. If you want to learn more about the DatabaseMetaData class and its methods, you can always visit this page. Also, I have pushed the code that’s used in this tutorial to GitHub for your reference. If you still have any issues connecting to your database using Progress DataDirect JDBC drivers, leave your comments below or contact support.
In the next part of this JDBC-Revisited tutorial series, we will show you how Progress DataDirect JDBC drivers offer database interoperability features as well as how they can make the life of developer easier. Subscribe to our blog via Email or RSS feed for updates to this tutorial series.Saikrishna is a DataDirect Developer Evangelist at Progress. Prior to working at Progress, he worked as Software Engineer for 3 years after getting his undergraduate degree, and recently graduated from NC State University with Masters in Computer Science. His interests are in the areas of Data Connectivity, SaaS and Mobile App Development.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites