Question

I've created a webapp with eclipse kepler and i'm using tomcat 7.0.

When i run the app with eclipse, the connection to my DB works fine but when i try to run it with tomcat i have no connection to my DB.

Here is the java class which creates the connection:

public class ConexionBD {

    private static String host = null;
    private static String db = null;
    private static String username = null;
    private static String password = null;

    private static void RecuperarDatosConexion() {

        try {
            Document d = new SAXBuilder().build(new File(System.getProperty("user.home")+ "/config.xml"));
            Element dbElement = d.getRootElement();

            host = dbElement.getChildText("host");
            db = dbElement.getChildText("db");
            username = dbElement.getChildText("username");
            password = dbElement.getChildText("password");

        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    public static Connection NuevaConexion() throws SQLException {
        RecuperarDatosConexion();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();            
        }

        StringBuilder ubicacionServicio = new StringBuilder();
        ubicacionServicio.append("jdbc:mysql://");
        ubicacionServicio.append(host);
        ubicacionServicio.append("/");
        ubicacionServicio.append(db);

        return DriverManager.getConnection (
                ubicacionServicio.toString(), username, password);
    }
}

And here is my config.xml code:

<?xml version="1.0" encoding="UTF-8"?>

<database>

  <host>localhost</host>

  <username>someuser</username>

  <password>somepassword</password>

  <db>stripped</db>

</database>

What am i doing wrong?

Was it helpful?

Solution

The issue here is the call to System.getProperty("user.home"), which will return different values based on the user that is executing the java application.

If you are starting tomcat using sudo, then tomcat is running as the root user, which means /root/ will be returned in the user.home property. So, you need to find out the user that is running tomcat and put the file in the directory under that specific user.

An alternative, more reliable approach would be to define those properties in the web.xml file as:

<context-param>
        <param-name>db.host</param-name>
        <param-value>localhost</param-value>
</context-param>
<context-param>
        <param-name>db.username</param-name>
        <param-value>someuser</param-value>
</context-param>
<context-param>
        <param-name>db.password</param-name>
        <param-value>somepassword</param-value>
</context-param>

Then you can access these properties in your servlets like below:

String databaseHost = getServletContext().getInitParameter("db.username"); 
String databaseUser = getServletContext().getInitParameter("db.username"); 
String databasePassword = getServletContext().getInitParameter("db.password");  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top