How to get the properties of a driver using JDBC?

JDBCObject Oriented ProgrammingProgramming

You can get the properties of a driver using the getPropertyInfo() method of the Driver interface.

DriverPropertyInfo[] info = driver.getPropertyInfo(mysqlUrl, null);

This method accepts a two parameters: A String variable representing the URL of the database, an object of the class Properties and, returns an array of the DriverPropertyInfo objects, where each object holds information about the possible properties of the current driver.

From a DriverPropertyInfo object you can get the information such as name of the property, value of the property, description, choices and, if it is required or not, using its fields name, value, description, choices, required, respectively.

DriverPropertyInfo[] info = driver.getPropertyInfo(mysqlUrl, null);
for (int i = 0; i < info.length; i++) {
   System.out.println("Property name: "+info[i].name);
   System.out.println("Property value: "+info[i].value);
   System.out.println("Property description: "+info[i].description);
   System.out.println("Choices: "+info[i].choices);
   System.out.println("Is Required: "+info[i].required);
   System.out.println(" ");
}

Following JDBC program establishes connection to MySQL database and retrieves the name, values, description and, choices of the required properties of the current driver.

Example

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
public class DriverPropertyinfoExample {
   public static void main(String args[]) throws Exception {
      String mysqlUrl = "jdbc:mysql://localhost/sampledatabase";
      //Creating a Driver object of the MySQL database
      Driver driver = DriverManager.getDriver(mysqlUrl);
      //Registering the Driver
      DriverManager.registerDriver(driver);
      //Getting the connection
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established: "+con);
      //Getting the properties of the current driver
      DriverPropertyInfo[] info = driver.getPropertyInfo(mysqlUrl, null);
      for (int i = 0; i < info.length; i++) {
         if(info[i].required) {
            System.out.print("Property name: "+info[i].name+", ");
            System.out.print("Property value: "+info[i].value+", ");
            System.out.print("Property description: "+info[i].description+", ");
            System.out.print("Choices: "+info[i].choices);
            System.out.println(" ");
         }
      }
   }
}

Output

Connection established: com.mysql.jdbc.JDBC4Connection@6d1e7682
Property name: HOST, Property value: localhost, Property description: Hostname of MySQL Server, Choices: null
Property name: user, Property value: null, Property description: Username to authenticate as, Choices: null
Property name: password, Property value: null, Property description: Password to use for authentication, Choices: null
raja
Published on 04-Jul-2019 15:49:15
Advertisements