0

Hi all following is my database connection file,

package org.slingemp.common;

import java.sql.Connection;
import java.sql.DriverManager;

public class JDBCManager {

    public Connection mysqlConnection()  {
        Connection dbConnection = null;
        try {

          Class.forName("com.mysql.jdbc.Driver");
          dbConnection=DriverManager.getConnection("jdbc:mysql://localhost:3306/slingemp","root","root");
          //System.out.println("mysql Driver Connedted::::::::");

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

    }
}

in this i want to make connection string and driver name configurable. how to do this and where to put the file which contains configurable values?

Regards Tony

5
  • 1
    Have you looked into using JNDI? I think that would be "standard" answer. Also, having your application connect as root is not advisable. Commented Jun 6, 2012 at 16:22
  • Thanks for the valuable point, i am using tomcat 7. is it possible to use JNDI, i am sorry i have used JNDI in jboss before so that i've asked this question Commented Jun 6, 2012 at 16:27
  • Yes, JNDI can be used with tomcat 7. See tomcat.apache.org/tomcat-7.0-doc/… This actually includes a step-by-step example for MySQL. Commented Jun 6, 2012 at 16:29
  • Thanks for patience and your valuable answers, how to do the following part in java not using jstl . 4. Test code of the reference you gave. Please do pardon me and spend some time Commented Jun 6, 2012 at 16:59
  • 1
    See step 3 of the Oracle example immediately following the MySQL example. Commented Jun 6, 2012 at 17:11

2 Answers 2

2

You can place these values in a properties file say database.properties file, then load those properties by using a code like below:

private void loadProperties(){
  InputStream inputStream = JDBCManager.class.getClassLoader().getResourceAsStream("database.properties");
  try {
            databaseProperties.load(inputStream); // database properties is an instance variable Properties databaseProperties;
        } catch (IOException e) {
            logger.error("Exception occurred while loading server properties", e);
        }
}

As per comments:

Yes, JNDI can be used in Tomcat, for configuring datasource on Tomcat 7 please read this official documentation

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

Comments

1

You can put them into .properties file or server JNDI resources.

jdbc.properties example:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yourDatabaseName
jdbc.username=username
jdbc.password=password

Then in java class use this to get properties:

public static ResourceBundle getJdbcBundle() {
    return ResourceBundle.getBundle("jdbc");
}

String url = SomeClass.getJdbcBundle().getString("jdbc.url");

But more efficient way would be to use JNDI.

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.