How to get JDBC Driver Property Info in Java?

DeeKay April 29, 2011 at 5:42 pm · java.sql

Below java code shows you JDBC driver property information –


/****************************************************************************************
 * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javasql;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.util.Properties;

/**
 * Created by http://kodehelp.com
 * Date: 4/29/11
 */
public class GetDriverPropertyInfo {
    public static void main(String args[]) throws Exception{

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Properties info = new Properties();
        Driver driver = DriverManager.getDriver("jdbc:oracle:thin:@HOST:PORT:SID");
        System.out.println("driver=" + driver);
        DriverPropertyInfo[] attributes = driver.getPropertyInfo("jdbc:oracle:thin:@HOST:PORT:SID", info);
        System.out.println("attributes=" + attributes);
        // zero length means a connection attempt can be made
        System.out.println("Resolving properties for: " + driver.getClass().getName());

        for (int i = 0; i < attributes.length; i++) {
            // get the property metadata
            String name = attributes[i].name;
            String[] choices = attributes[i].choices;
            boolean required = attributes[i].required;
            String description = attributes[i].description;
            // printout property metadata
            System.out.println(name + " (Required: " + required + ")");
            if (choices == null) {
             System.out.println(" No choices.");
            } else {
             System.out.print(" Choices are: ");
             for (int j = 0; j < choices.length; j++) {
              System.out.print(" " + choices[j]);
             }
            }
            System.out.println(" Description: " + description);
        }
    }
}