How Do You Create a DriverPropertyInfo Object? JDBC

The java.sql.Driver.getPropertyInfo() method returns an array of DriverPropertyInfo objects (DriverPropertyInfo[]). JDBC’s Driver.getPropertyInfo() method allows a GUI tool to determine the database connection properties for which it should prompt a user in order to get enough information to connect to a database. Depending on the values the user has supplied so far, additional values might become necessary. It might be necessary to iterate through several calls to getPropertyInfo(). If no more properties are necessary (i.e., all required parameters are specified), the call returns an array of zero length. In general, Driver.getPropertyInfo() offers java.sql.Connection choices to the user.

How It Works

For most database systems, the properties returned in the DriverPropertyInfo object are connection URL attributes, including a list of booted databases in a system (the databaseName attribute). When a nonzero-length array (DriverPropertyInfo[]) is returned by the Driver. getPropertyInfo() method, each element is a DriverPropertyInfo object representing a connection URL attribute that has not already been specified.

The signature of the Driver.getPropertyInfo() method is

* @param url - the URL of the database to which to connect
* @param info - a proposed list of tag/value pairs that will
* be sent on connect open
* @return Returns an array of DriverPropertyInfo objects
* describing possible properties. This array may be
* an empty array if no properties are required.
* @throws SQLException - if a database access error occurs
DriverPropertyInfo[] getPropertyInfo(java.lang.String url,
java.util.Properties info)
throws java.sql.SQLException

I’ll describe the Driver.getPropertyInfo() method next. This method gets information about the possible properties for this driver. The getPropertyInfo() method is intended to allow a generic GUI tool to discover what properties it should prompt the user for in order to get enough information to connect to a database. Note that depending on the values the user has supplied so far, additional values may be required, so it may be necessary to iterate through several calls to the getPropertyInfo() method. According to JDBC API Tutorial and Reference, Third Edition (Addison Wesley Publishing, August 1997):

The second argument should be null the first (and generally only) time this method is called. The second argument is included so that it is possible for an application to process input and present the human user with a list of properties from which to choose. Depending on the values the human has supplied so far, it is conceivable that additional values may be needed. In such cases, and assuming that the application has been written to handle the results of multiple calls to getPropertyInfo(), it may be necessary to iterate through several calls to getPropertyInfo(). If the application uses the information it gets to fill in values for a java.util.Properties object, then that object can be supplied to getPropertyInfo() in subsequent calls to the method. The properties returned by this method will often correspond to the properties that are set on a javax.sql.DataSource object for this driver.

The Solution

Here is some example code for a MySQL/Oracle database. This solution displays Driver property information as an XML serialized String object. In this solution, I check for drivers from Oracle, MySQL, and JDBC-ODBC. You may alter the code and add additional drivers. To do this, modify the loadDriver() method.

import java.sql.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.sql.DriverPropertyInfo;
import jcb.util.DatabaseUtil;
public class TestDriverPropertyInfo {
// list of drivers to be tested.
public static final String MYSQL_DRIVER =
public static final String ORACLE_DRIVER =
public static final String JDBC_ODBC_BRIDGE_DRIVER =
public static void loadDriver(String dbVendor) throws Exception {
if (dbVendor.equalsIgnoreCase("mysql")) {
Class.forName(MYSQL_DRIVER); // load MySQL driver
else if (dbVendor.equalsIgnoreCase("oracle")) {
Class.forName(ORACLE_DRIVER); // load Oracle driver
else if (dbVendor.equalsIgnoreCase("jdbc-odbc")) {
// load JdbcOdbcDriver
else {
throw new Exception("db vendor not supported");
public static String getDriverPropertyInfoAsXML
(DriverPropertyInfo[] properties) throws Exception {
// If the driver is poorly implemented,
// a null object may be returned.
if(properties == null) {
return null;
// List all properties.
StringBuilder buffer = new StringBuilder();
for(int i = 0; i < properties.length; i++) {
// Get the property metadata
String name = properties[i].name;
boolean required = properties[i].required;
buffer.append("<property name=\"");
buffer.append("\" required=\"");
String value = properties[i].value;
String description = properties[i].description;
String[] choices = properties[i].choices;
if(choices != null) {
for(int j = 0; j < choices.length; j++) {
return buffer.toString();
public static void main(String[] args)throws Exception {
String dbVendor = args[0]; // { "mysql", "oracle" }
// start with the least amount of information
// to see the full list of choices; we could also
// enter with a URL and Properties provided by a user.
// mysql URL = "jdbc:mysql://localhost/octopus";
// oracle URL = "jdbc:oracle:thin:@localhost:1521:caspian";
// JdbcOdbc URL = "jdbc:odbc:northwind";
String url = args[1]; // database url
Properties info = new Properties();
Driver driver = DriverManager.getDriver(url);
DriverPropertyInfo[] attributes =
driver.getPropertyInfo(url, info);
// zero length means a connection attempt can be made
System.out.println("Resolving properties for: " +
// you can insert code here to process the array, e.g.,
// display all options in a GUI and allow the user to
// pick and then set the attributes in info or URL.
// try the connection
Connection conn = DriverManager.getConnection(url, info);

Running the Solution for a MySQL Database

The following output is formatted to fit the page:

$ javac
$ java TestDriverPropertyInfo mysql "jdbc:mysql://localhost/octopus"
Resolving properties for: com.mysql.jdbc.Driver
<property name="HOST" required="true">
<description>Hostname of MySQL Server</description>
<property name="PORT" required="false">
<description>Port number of MySQL Server</description>
<property name="DBNAME" required="false">
<description>Database name</description>
<property name="user" required="true">
<description>Username to authenticate as</description>
<property name="password" required="true">
<description>Password to use for authentication</description>
<property name="autoReconnect" required="false">
<description>Should the driver try to re-establish bad
<property name="maxReconnects" required="false">
<description>Maximum number of reconnects to at
tempt if autoReconnect is true</description>
<property name="initialTimeout" required="false">
<description>Initial timeout (seconds)to wait between failed
<property name="profileSql" required="false">
<description>Trace queries and their execution/fetch times
on STDERR (true/false) defaults to false</description>


Running the Solution for an Oracle Database

$ java TestDriverPropertyInfo oracle "jdbc:oracle:thin:@localhost:1521:caspian"
Resolving properties for: oracle.jdbc.driver.OracleDriver

As you can see from the preceding output, the Oracle driver does not properly return an array of DriverPropertyInfo objects, but instead returns an empty array.

Running the Solution for a JDBC-ODBC Configured Database

$ java TestDriverPropertyInfo "jdbc-odbc" "jdbc:odbc:northwind"
Exception in thread "main" java.sql.SQLException:
[Microsoft][ODBC Driver Manager] Driver does not support this function
at sun.jdbc.odbc.JdbcOdbc.createSQLException(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLBrowseConnect(
at sun.jdbc.odbc.JdbcOdbcDriver.getConnectionAttributes
at sun.jdbc.odbc.JdbcOdbcDriver.getPropertyInfo
at TestDriverPropertyInfo.main(

As the preceding output shows, the JDBC-ODBC bridge driver does not properly return an array of DriverPropertyInfo objects, but instead throws a java.sql.SQLException. The MySQL database driver works better in this respect.

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

JDBC Topics