Java如何获取JDBC驱动程序属性信息?
下面的示例向您展示如何获取有关相应JDBC驱动程序可能的属性的信息。
package org.nhooo.example.jdbc; import java.sql.*; public class DriverPropertyInfoDemo { private static final String URL = "jdbc:mysql://localhost/nhooo"; public static void main(String[] args) { try { //获取有关此属性的信息 //司机。 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(); } } }
上面的代码片段的示例结果:
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 : nhooo ----------------------------------- Name : allowLoadLocalInfile Description: Should the driver allow use of 'LOAD DATA LOCAL INFILE...' (defaults to 'true'). Value : true Choices : true,false,yes,no, -----------------------------------
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>