在JDBC URL中不使用主机名和端口 [英] Not using the hostname and port in jdbc url

查看:424
本文介绍了在JDBC URL中不使用主机名和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些应用程序通过SpringJDBC和普通JDBC连接到Oracle数据库.

I have some application connecting to Oracle database through SpringJDBC as well as plain JDBC.

JDBC URL模式是-

JDBC URL pattern is -

jdbc:oracle:thin:@hostname:portNumber/schemaName

我们正在尝试创建一个与应用程序相关的TNS条目.例如,如果有一个应用程序StockTrade,则架构名称将为DBStockTrade.

We are trying to create a TNS entry which can be related with the application. For example if there is an application StockTrade then the schema name will be DBStockTrade.

但是,开发人员仍然需要知道用于构造JDBC URL的主机名和端口号,并且每当更改架构名称时都需要进行二进制更改.

However developers still need to know the hostname and port number for construction of JDBC URL and need to make binary change whenever the schema name is changed.

有没有一种方法可以避免使用主机名和端口号,而只需使用架构名称来连接数据库?想法是使用某种仅包含模式名称的属性文件,而摆脱其他详细信息.

Is there a way to avoid using the hostname and port number and simply use the schema name for connecting to the database? The idea is to use some kind of property file containing the schema name only and get rid of other details.

推荐答案

针对该问题:

每当更改架构名称时都需要进行二进制更改

need to make binary change whenever the schema name is changed

我们只需将JDBC URL放入配置文件

然后,只要更改架构名称,我们只需要编辑配置文件,而无需二进制更改.

Then whenever the schema name changed, we only need to edit the configuration file and no binary change needed.

例如,将JDBC URL保存到 .properties 文件

For example, save the JDBC URL to a .properties file

# config.properties

jdbc_url=jdbc:oracle:thin:@hostname:portNumber/schemaName

在Java代码中:

String currentPath = this.getClass().getProtectionDomain().getCodeSource()
    .getLocation().toURI().getPath();
String configFile = currentPath + File.separator + "config.properties";
Properties prop = new Properties();
prop.load(new FileInputStream(configFile));
String jdbcUrl = prop.getProperty("jdbc_url")

因此,通过jdbcUrl,我们可以连接到数据库

So with the jdbcUrl we can connect to the database

这篇关于在JDBC URL中不使用主机名和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆