在JDBC URL中不使用主机名和端口

编程入门 行业动态 更新时间:2024-10-28 08:28:24
本文介绍了在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中不使用主机名和端口

本文发布于:2023-10-17 03:05:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1499594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   端口   主机名   JDBC   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!