0

I'm new to programming in Java. I have a SQL connection class that connects to my database.

I got my code reviewed by my mentor and he asked me to "Externalize strings using properties file". I am not sure what he means by this and how to go about doing this.

I researched this online and found articles about eclipse wizard and internationalization. This has left me more confused.I'm not sure if I should follow it.

My SQL Connection class looks like this.

public class SQLConnection {
    Connection conn = null;
    public static Connection dbConnector() {
        try {
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:xxxdbnamexxx.db");

            return conn;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
}

I expect connection class to return connections like it usually does after the externalization of the strings.

I use Eclipse IDE if that helps.

2 Answers 2

3

Your mentor means that your application should get the strings "jdbc:sqlite:xxxdbnamexxx.db" and possibly "org.sqlite.JDBC" from a property file (or similar) rather than hard-wiring them into your code.

This will allow the user of your application to connect to a different database without modifying the source code. All they need to do is to modify the property file containing the config properties.

Now it is debatable what precisely needs to be externalized. One thing to consider is that your code could be SQLite specific, either because the database will always be SQLite, or because you are relying on SQLite-specific SQL or behaviors. So it is unclear if the driver class name ("org.sqlite.JDBC") should be a parameter.

There are many possible ways to do the externalization, but the simple way is to use a java.util.Properties object and its load and save methods; see the javadocs for details.


This is not related to internationalization, where the application is getting user messages from a "resource bundle" depending on the locale in which the application is running.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Stephen, thanks for your answer. The let's assume the SQL behavior is not specific to SQLite. How should I configure this? I was thinking abou making a text file outside src with key:value pairs, and writing a static method in my utility class to return value by passing key. I will then call this method where ever I want to pass strings. Is this the correct practice?
0

I'm not sure whether there is a trivial way to do that. But you can do with this:

//Properties is a class:
Properties prop=new Properties();
//read file
InputStream in = 
BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
prop.load(in);
//load file
String userName = prop.getProperty("userName");
String pwd = prop.getProperty("pwd");
String url = prop.getProperty("url");
String driver = prop.getProperty("driver");
// database driver
Class.forName(driver);
// get connection
Connection conn = DriverManager.getConnection(url, userName, pwd);

And create a new file named 'jdbc.properties', placed in the root of resource directory:

userName=root
pwd=password
// sqlite driver name
driver=org.sqlite.JDBC
// address of your database 
url=jdbc:sqlite:personName.db

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.