用于在Java中保存连接字符串参数的配置文件 [英] Config file for holding connection string parameters in Java

查看:528
本文介绍了用于在Java中保存连接字符串参数的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自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.

我有的问题是,我有所有的连接属性harcoded到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.

推荐答案

最简单的方法,如果你不绑定到一个特定的框架,与属性文件。请查看此问题的接受答案的后半部分(不是您的情况下的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中保存连接字符串参数的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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