java - 数据源属性

标签 java jdbc

为什么DataSource 接口(interface)未定义 JDBC 指定的标准数据源属性 - DataSource

使用 DataSource 类型的引用 ds,我无法像这样设置以下属性:

public DbUtil(DataSource ds, String dbName, String port){
  ds.setServerName(dbName);
  ds.setPortNumber(port); 
}

最佳答案

因为您不应该传入这些值,这与普通的旧DriverManager不同。数据源通常在容器上配置,并且只能使用 JNDI 进行查找。

如果您再次开始传递这些值,则会破坏可配置数据源的全部意义。

这是来自 API 的引用。

The DataSource interface is implemented by a driver vendor.

A DataSource object has properties that can be modified when necessary. For example, if the data source is moved to a different server, the property for the server can be changed. The benefit is that because the data source's properties can be changed, any code accessing that data source does not need to be changed.

关于java - 数据源属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10687453/

相关文章:

java - 解析 JSON 响应?

java - 如何在使用堆栈时反转字符串中的单个单词?

sql-server - 无法使用 Jmeter 创建类 'com.microsoft.sqlserver.jdbc.SQLServerDriver' 的 JDBC 驱动程序

java - 即使显式设置,PreparedStatement 也永远不会超时

java - Oracle clob 上的 union\minus 时发生问题

java - header 中包含用户名和密码的 SOAP 客户端请求

Java/Spring : How to Instatiate an Object on Server Startup

java - primefaces 具有可扩展行+可编辑扩展的数据表

java - 无法从 Statement.executeQuery() 获取选定的列

java - 如何进行始终返回零行的选择