星期二, 三月 08, 2005

如何得到JDBC支持的连接字符串的属性?

得到一个JDBC支持的连接属性的list

import java.sql.*;

public class listAllJDBCParameters
{
public static void main(String[] args)
{
try {
// Load the driver
String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
if(args.length>0)driverName=args[0];
// Get the Driver instance
String url = "jdbc:mysql://localhost:3306/PCD55";
Driver driver = DriverManager.getDriver(url);

// Get available properties
DriverPropertyInfo[] info = driver.getPropertyInfo(url, null);
System.out.println("属性个数:"+info.length);
for (int i=0; i<info.length; i++) {
// Get name of property
String name = info[i].name;
System.out.print(name+":");
// Is property value required?
boolean isRequired = info[i].required;
System.out.print(isRequired);
// Get current value
String value = info[i].value;
System.out.print(":"+value);
// Get description of property
String desc = info[i].description;
System.out.println(":"+desc);
// Get possible choices for property; if null, value can be any string
String[] choices = info[i].choices;
}
} catch (ClassNotFoundException e)
{
// Could not find the database driver
} catch (SQLException e)
{
}
}
}