How to get JDBC Driver Property Info in Java?

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);
        }
    }
}

4 comments

  1. Javin Paul says:

    Excellent article , you have indeed covered topic in details with code examples and explanation. I have also blogged some of my experience as 10 tips on logging in Java

    Thanks
    Javin
    10 tips on logging in Java

  2. Javin Paul says:

    Excellent article , you have indeed covered topic in details with code examples and explanation. I have also blogged some of my experience as 10 tips on logging in Java

    Thanks
    Javin
    10 tips on logging in Java

  3. dharmendra patel says:

    thanks for sharing this short but very informative article…

    Regards
    Dharmendra

  4. dharmendra patel says:

    thanks for sharing this short but very informative article…

    Regards
    Dharmendra

Please Post Your Comments & Reviews

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.