What Are a Table's Column Names? in JDBC

When you’re building SQL adapters and database GUI applications, keep in mind that clients might be interested in viewing and selecting columns (and their associated data types). Before you insert new records into a table, you might want to check the table columns’ associated types. Doing so can prevent redundant network traffic.

You can use DatabaseMetaData.getColumns() to get list of columns for a table or view. In production environments, try to minimize passing null/empty parameter values to this method. Passing non-null and non-empty parameter values to JDBC metadata methods can improve the overall performance of your applications.

getColumnNames()

// Oracle requires table names to in uppercase characters // MySQL is case-insensitive to table names

Test Program

The following test program prints a listing of the column names for the table MyPayrollTable:

// // print the list of column names for table MyPayrollTable //

Output of the Test Program

The name and type combination provides information about the table schema, but it is not enough. You need to get other useful information, such as the size of the column and whether the column is nullable. (Nullable means that the column accepts the NULL value; note that NULL in SQL is not a zero or an empty value but instead indicates that the value is missing.) So, you can modify the program to provide more detailed information for each column. Since you are returning four distinct pieces of information for each column, you will return the result as an XML String object. For each column, the following information will be returned:

Testing getColumnDetails()

// // print the detail of columns for table TestTable77 //

Output of Testing getColumnDetails()

33faf4ff068d72f2adcfa053cf4f7274

getColumnDetails() Method

getColumnDetails() Method: Selecting Type, Size, Nullable, Position

The following method selects the specific metadata that you are interested in by passing a boolean flag for each metadata:



All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics