0

I'm trying to set my database property, so in the future users could change it if they want to change the route of the database. The problem is when I try to set something with full width colon, which always add a backslash escape character .

I've tried normal and double escape, but it doesn't work.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class SetProps {

public static void SetDefaultProps(){
    Properties prop = new Properties();
OutputStream output = null;

try {

    output = new FileOutputStream("./build/classes/configuracion.properties");

    // Set the database property
    prop.setProperty("url", "jdbc:mysql://192.168.1.192:3306/ordenestaller");


// Save Properties
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
}
public static void main(String[] args) {
        SetDefaultProps();
    }
}
5
  • You don't need to escape anything in the mySQL URL, a colon is a regular character May 21, 2014 at 10:49
  • Function setProperty always add to : the backslash /:
    – Luis A.G.
    May 21, 2014 at 11:04
  • Ist is not the 'setProperty` - Method. It is the store Method that will add the //
    – Jens
    May 21, 2014 at 11:09
  • you give me the point! @Jens
    – Luis A.G.
    May 21, 2014 at 11:34
  • Got the solution from here: stackoverflow.com/questions/17990740/… There is no problem with that backslash, properties file consider full width colon as a special character so it's needed. Also, properties.load() returns the information without that backslashes. Thanks for the help
    – Luis A.G.
    May 21, 2014 at 11:38

1 Answer 1

0

Hardcoding the db url in the java code is not a good coding practice. I would recommend you to put those urls in a properties files and read them as and when required using a ResouceBundle.

you can have "information.properties" file somewhere in your classpath and the contents of that file can be

dbURL:jdbc:mysql://192.168.1.192:3306/ordenestaller

and now in your code you can get this using a ResourceBundle

ResourceBundle bundle = ResourceBundle.getBundle("information");
String dbURL= bundle.getString("dbURL");

This will have an added advantage that you will not need to recompile your java class again if DB URl is changed.

Hope it helps

1
  • Thank you very much, but I need to type the full width colon because I'm setting also directory path and the C:/Documents and Settings.... is adding the backlash again C\:/
    – Luis A.G.
    May 21, 2014 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.