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
John
  • 115
  • 2
  • 3
  • 10
  • 1
    Load the properties file using `getResourceAsStream()` . Check [this](http://stackoverflow.com/questions/4814506/properties-file-in-web-app). – AllTooSir Jul 23 '13 at 07:51
  • use this InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream("dbConnection.properties"); – Veera Jul 23 '13 at 07:54

4 Answers4

1

Instead of

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

use

InputStream in = getClass().getResourceAsStream("dbConnection.properties");
Mark M
  • 1,580
  • 10
  • 22
  • in both of instruction 'in' will be null ! – John Jul 23 '13 at 08:21
  • 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 – John Jul 23 '13 at 08:28
  • 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. – Mark M Jul 23 '13 at 08:45
  • 1
    `getClass().getClassLoader().getResourceAsStream("dbConnection.properties")` should work – nickRise Apr 24 '17 at 15:50
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();
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
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"

mel3kings
  • 8,857
  • 3
  • 60
  • 68
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'

KingFeming
  • 1,053
  • 10
  • 21