Config file for holding connection string parameters in Java

前端 未结 2 1169
眼角桃花
眼角桃花 2021-01-13 07:56

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 i

相关标签:
2条回答
  • 2021-01-13 08:33

    As far dealing with properties/property files, I haven't really seen a standard defined for it in Java. But since you are beginning with Java, you could start looking at. Apache Commons Config User Guide. It provides generic configuration interface that can be used to load properties from variety of sources.

    I recommend going with Spring Framework. Spring is de-facto standard Javav application framework. The following link Spring New Config Features should set you up most some of most innovative approaches to work 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 capabilities to deal with properties, uses annotations @Value to and automatically inject required properties. Besides this Spring abstracts your property source details from your files.

    0 讨论(0)
  • 2021-01-13 08:44

    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).

    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.

    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.

    0 讨论(0)
提交回复
热议问题