What properties should I supply to a database driver in order to connect to a database?
Created May 4, 2012
Lennart Jorelid
Most JDBC drivers should accept 3 properties:
- user
- password
- hostname
However, a JDBC driver may accept an arbitrary number of properties thrown at it. Drivers can be interrogated for their supported properties using the DriverPropertyInfo metadata class. Most drivers will also contain documentation which should specify all properties and their meaning for creating the jdbc database connection.
NOTE! The JDBC/ODBC bridge driver does not properly return an array of DriverPropertyInfo objects, but instead throws a NullPointerException. Other database drivers work better in this respect.
public static void printPropertyInfo(Driver aDriver, String jdbcURL, Properties daProps) throws Exception { // Get the DriverPropertyInfo of the given driver DriverPropertyInfo[] props = aDriver.getPropertyInfo(jdbcURL, daProps); // If the driver is poorly implemented, // a null object may be returned. if(props == null) return; System.out.println("Resolving properties for: " + aDriver.getClass().getName()); // List all properties. for(int i = 0; i props.length; i++) { // Get the property metadata String propName = props[i].name; String[] propChoices = props[i].choices; boolean req = props[i].required; String propDesc = props[i].description; // Printout System.out.println("" + propName + " (Req: " + req + ")"); if(propChoices == null) { System.out.println(" No choices."); } else { System.out.print(" Choices: "); for(int j = 0; j propChoices.length; j++) { System.out.print(" " + propChoices[j]); } } System.out.println(" Desc: " + propDesc); } } } |