2

I write a code to connect to database from servlet.I want to use properties.but it does not work.i think i my code or properties file has problem.please help me to correct it.

try
        {
            Properties prop=new Properties();
            FileInputStream in = new FileInputStream(System.getProperty("WEB-INF/dbConnection.properties"));
            prop.load(in);
            in.close();

            String drivers = prop.getProperty("jdbc.drivers");
            String connectionURL = prop.getProperty("jdbc.url");
            String username = prop.getProperty("jdbc.username");
            String password = prop.getProperty("jdbc.password");
            Class.forName(drivers);
            con=DriverManager.getConnection(connectionURL,username,password);
            System.out.println("Connection Successful");

..

and this is my properties file

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.101.84:1521:orcl
jdbc.username=user1
jdbc.password=123
2
  • 1
    Load the properties file using getResourceAsStream() . Check this. Commented Jul 23, 2013 at 7:51
  • use this InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream("dbConnection.properties"); Commented Jul 23, 2013 at 7:54

4 Answers 4

1

Instead of

FileInputStream in = new FileInputStream(System.getProperty("WEB-INF/dbConnection.properties"));

use

InputStream in = getClass().getResourceAsStream("dbConnection.properties");
Sign up to request clarification or add additional context in comments.

4 Comments

in both of instruction 'in' will be null !
i create a dbConnection.properties in WEB-INF and put my info. there.so when i debug my project when it Achieve to this command it puts nothing in "in" and give me exception
Try removing WEB-INF/ and see what happens. Or try to find the actual path, I can't see your system so I can't help on that part.
getClass().getClassLoader().getResourceAsStream("dbConnection.properties") should work
1
try  {
    FileReader reader = new FileReader("Full Path");
    Properties prop = new Properties();
    prop.load(reader);

    String drivers = prop.getProperty("jdbc.driverClassName");
    String connectionURL = prop.getProperty("jdbc.url");
    String username = prop.getProperty("jdbc.username");
    String password = prop.getProperty("jdbc.password");
    Class.forName(drivers);
    Connection con = DriverManager.getConnection(connectionURL, username, password);
    System.out.println("Connection Successful");
} catch (SQLException sqle) {
    // TODO: Add catch code
    sqle.printStackTrace();
} catch (FileNotFoundException fnfe) {
    // TODO: Add catch code
    fnfe.printStackTrace();
} catch (IOException ioe) {
    // TODO: Add catch code
    ioe.printStackTrace();
} catch (ClassNotFoundException cnfe) {
    // TODO: Add catch code
    cnfe.printStackTrace();
}catch (Exception e) {
    // TODO: Add catch code
    e.printStackTrace();
}

Comments

0

This part:

(System.getProperty("WEB-INF/dbConnection.properties")); 

is actually referring to your classpath, try removing WEB-INF/ or just leave /, for checking root folder.

Or you can specify the fully qualified name, i.e : "C:\\foo\\test\\...\\dbConnection.properties" or "/app/blah/.../dbConnectionProperties"

Comments

0

For Class name, in the properties file you have defined the driver as 'jdbc.driverClassName'

but when passing variable you just pass as 'jdbc.drivers'

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.