简体   繁体   中英

How to externalize strings in the SQL Connection class

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.

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.

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

DriverManager.html#getConnection is an overloaded method. You are using the simple version that accepts a single string. Other versions exists, for example one that accepts the URL, the username and the password .

You can use it like this:

// The three arguments are here just for demonstration. Read them from a file,
// pass them as environment variables or as user input.
String url = "jdbc:sqlite:xxxdbnamexxx.db";
String username = "dbuser";
String password = "secret-password";

Connection conn = DriveManager.getConnection(url, username, password);

You should not put these three strings directly inside your code. Put it into an external file that your code reads, or pass them as environment variables to your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM