I am trying to make a connection with database from a class called ConnectionProvider
.
I call ConnectionProvider.getConnection()
inside Servlet
which return Connection
refrence.
But I get exception during execution of this class:
java.io.FileNotFoundException: db.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
code of ConnectionProvider:
public class ConnectionProvider {
static Properties prop;
static {
try{
prop= new Properties();
prop.load(new FileInputStream("db.properties"));
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection con=null;
try{
Class.forName(prop.getProperty("driver"));
con=DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("user"),prop.getProperty("pass"));
}catch(Exception e){
e.printStackTrace();
}
return con;
}
}
I cannot use ServletContext.getResourceAsStream() bcause code is not called from servlet. I have tried to put property file in each and every location inside public folder (root) of web app but that doesn't help. I also donot want to hardcode the connectivity code inside Servlet and want some helper class to perform the connectivity code for me.