Configuration file for storing connection string parameters in Java

I come from the background of ASP.Net. Now I am writing a Java program to import data from a DB2 database into an Oracle database. I performed the main functions of importing this data.

I have a problem: I have all the connection properties recorded in the Java program itself. Is there any Best Practice methodology that contains connection string data stored in a configuration file like web.config that we use for ASP.Net? Does Java have any mechanism for this? Or do I just need to use I / O to read key / value pairs from a simple txt file? I read a little about .properties? So is it going?

I'm just looking for a product in the right direction. Any help would be appreciated.

+4
source share
2 answers

The easiest way to do this if you are not tied to a specific structure is with a properties file. Take a look at the last part of the accepted answer to this question (and not the XML bit for your case).

Depending on whether you use a servlet container or similar (e.g. Tomcat, Glassfish, etc.), you might also consider using container connection settings and a connection pool, which might be easier than recycling your own connections.

But if you are just trying to feel your way in Java and you need to learn about JDBC, first try it simply and simply read your properties from the db.properties file in your class path.

+4
source

Since I am dealing with property / property files, I have not really seen the standard defined for it in Java. But since you are starting with Java, you can start looking. Apache Commons Config User Guide . It provides a common configuration interface that you can use to load properties from various sources.

I recommend migrating from the Spring Framework . Spring is the de facto standard Javav application database. Next Spring link New configuration options should set most of the most innovative approaches to working with properties.

@Configuration public class AppConfig { private @Value("#{jdbcProperties.url}") String jdbcUrl; private @Value("#{jdbcProperties.username}") String username; private @Value("#{jdbcProperties.password}") String password } 

It has some advanced features for working with properties, uses @Value to annotations, and automatically enters the required properties. In addition, this Spring abstracts your source data of your resource from your files.

+2
source

Source: https://habr.com/ru/post/1392410/


All Articles