問題描述
我來自 ASP .Net 背景.我現在正在編寫一個 Java 程序來將數據從 DB2 數據庫導入 Oracle 數據庫.我已經完成了導入這些數據的基本功能.
I come from an ASP .Net background. I am now writing a Java program to import data from a DB2 database into an Oracle database. I have completed the basic functionality of importing this data.
我的問題是,我將所有連接屬性都硬編碼到 Java 程序本身中.是否有任何最佳實踐"方法可以將連接字符串詳細信息存儲在類似于我們用于 ASP .Net 的 web.config 的配置文件中?Java 是否有任何機制來實現這一點?還是我需要簡單地使用 I/O 操作從一個簡單的 txt 文件中讀取鍵/值對?我讀過一些關于 .properties 的內容?這是要走的路嗎?
The problem I have is, I have all the connection properties harcoded into the Java program itself. Is there any "Best Practices" methodology to have the connection string details stored in a configuration file similar to web.config we use for ASP .Net? Does Java have any kind of mechanism for accomplishing this? Or do I need to simply use I/O operations to read the key/value pairs from a simple txt file? I have read a bit about .properties? Is this this way to go?
我只是在尋找正確的方向.任何幫助將不勝感激.
I am just looking for a prod in the right direction. Any help would be appreciated.
推薦答案
如果您不依賴于特定框架,最簡單的方法是使用屬性文件.查看this question的已接受答案的后半部分(不是您的情況的 XML 位).
The simplest way to do it, if you're not tied to a particular framework, is with a properties file. Have a look at the latter part of the accepted answer for this question (not the XML bit for your case).
根據您是否使用 servlet 容器或類似容器(例如 Tomcat、Glassfish 等),您可能還希望考慮使用該容器的連接設置和連接池,這可能比回收您自己的連接更簡單.
Depending on whether you're using a servlet container or similar (e.g. Tomcat, Glassfish, etc.), you may also wish to consider using that container's connection settings and connection pooling, which may be simpler than recycling your own connections.
但是,如果您只是想嘗試進入 Java 并且這是為了讓您了解 JDBC,請先保持簡單,然后從類路徑上的 db.properties 文件中讀取您的屬性.
But if you're just trying to feel your way into Java and this is for you to learn about JDBC, keep it simple initially and just read your properties from a db.properties file on your classpath.
這篇關于用于在 Java 中保存連接字符串參數的配置文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!