Question

In my spring project, I have in my classpath a file named database.properties with the following content:

jdbc.Classname=org.postgresql.Driver
jdbc.url=
jdbc.user=
jdbc.pass=
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=validate

I have a method in one of my service classes where I manually export the database schema to server through hibernate. The code for this method in this moment is this:

public void create_tables(String maquina, String usuario, String senha) {
    Configuration config = new Configuration();
            SchemaExport schema = new SchemaExport(config);
            schema.create(true, true);
}

I want load the properties from file database.properties in my config variable, set up the values I pass to the method in this variable (url, user and pass), and save this new configuration in the same file.

Anyone can point the direction to do that?

Was it helpful?

Solution

does this do it?

Properties props = new Properties();
FileInputStream fis = new FileInputStream( "database.properties" );
props.load( fis );
fis.close();

props.setProperty("jdbc.url", {{urlvalue}} );
props.setProperty("jdbc.user", {{user value}} );
props.setProperty("jdbc.pass", {{pass value}} );

FileOutputStream fos = new FileOutputStream( "database.properties" );
props.store( fos );
fos.close();

OTHER TIPS

I have in my classpath a file named database.properties

I want load the properties from file database.properties in my config variable, set up the values I pass to the method in this variable (url, user and pass), and save this new configuration in the same file.

This is at least difficult, and maybe impossible.

The "file" you are trying to update may not be a file at all. It might be a component of a larger JAR or ZIP file. It may be an in-memory or on-disk cache of something that was downloaded. It might (hypothetically) have been encrypted using a public/private key ... for which we don't have the "encrypt" key.

In addition to being difficult, it is a bad idea. Suppose that the your service is deployed as a WAR file, and that the properties file is delivered in the WAR. You modify the properties ... and so on. Then, for some reason you redeploy the WAR. This will overwrite your configuration.

If you want the configuration properties to be updatable, they should not be on the classpath. Put the file into a separate directory (outside of the webapp tree ...) and access it via a file pathname or file: URL.


I try remove the classpath:, but I face the error Caused by: java.io.FileNotFoundException: class path resource [database.properties] cannot be opened because it does not exist

It looks like you are using an (incorrect) relative path for the properties file.

Copy the file to (say) "/tmp/database.properties", change the annotation to

    @PropertySource("/tmp/database.properties")

and see if that works. If it does, then you can figure out a more appropriate place to store the file. But as I said above, if you try to update a file in your webapp directory, there's a good chance it will get clobbered when you redeploy. So don't put it there.

Example load props from file. We have test config file with filename = conf.props Contains:

key0=value0
key1=value1

Next class load properties in the application:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class LogProcessor {



    public void start(String fileName ) throws IOException {

        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(fileName);
        prop.load(fis);

        System.out.println (prop.getProperty("key0"));
    }
}

How to run:

public static void main(String[] args) {

        try {
            new LogProcessor().start();
        } catch (IOException e) {
            e.printStackTrace();
        }

Return value0

\|/ 73

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top