How do I get JDBC driver property information?

The example below show you how to get the information about the possible properties for the corresponding JDBC driver.

package org.kodejava.example.jdbc;

import java.sql.*;

public class DriverPropertyInfoDemo {
    private static final String URL = "jdbc:mysql://localhost/kodejava";

    public static void main(String[] args) {
        try {
            // Gets information about the possible properties for this
            // driver.
            Driver driver = DriverManager.getDriver(URL);
            DriverPropertyInfo[] props = driver.getPropertyInfo(URL, null);

            for (DriverPropertyInfo info : props) {
                System.out.println("Name       : " + info.name);
                System.out.println("Description: " + info.description);
                System.out.println("Value      : " + info.value);
                System.out.println("-----------------------------------");

                String[] choices = info.choices;
                if (choices != null) {
                    StringBuilder sb = new StringBuilder("Choices    : ");
                    for (String choice : choices) {
                        sb.append(choice).append(",");
                    }

                    System.out.println(sb.toString());
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

The example result of the code snippet above:

Name       : HOST
Description: Hostname of MySQL Server
Value      : localhost
-----------------------------------
Name       : PORT
Description: Port number of MySQL Server
Value      : 3306
-----------------------------------
Name       : DBNAME
Description: Database name
Value      : kodejava
-----------------------------------
Name       : allowLoadLocalInfile
Description: Should the driver allow use of 'LOAD DATA LOCAL INFILE...' (defaults to 'true').
Value      : true
Choices    : true,false,yes,no,
-----------------------------------

Maven dependencies

<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.47</version>
</dependency>

Maven Central

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you šŸ„³

Leave a Reply

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