ShaunK
ShaunK

Reputation: 1221

properties file for JDBC

So my question is related to my previous post: SQLException : No value specified for parameter 1

I have no idea what a properties file is. Can I create a properties file from text editor, and have the path set in the catalina.properties file in the Tomcat/conf folder? or do I have the following

javabase.jdbc.url = jdbc:mysql://200.230.71.12:3306/social
javabase.jdbc.driver = com.mysql.jdbc.Driver
javabase.jdbc.username = cepein
javabase.jdbc.password = 1234cinco

inserted within shared.loader= in the catalina.properties file?

So anyway I did the following:

# starting with file:
shared.loader= /home/shaunkoo/NetBeansProjects/dao.properties

and load the file in via

private static final String PROPERTIES_FILE = "/NetBeansProjects/dao.properties";
private static final Properties PROPERTIES = new Properties();

static {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE);

However, I am getting the error that states that /NetBeansProjects/dao.properties is missing in classpath. Any clue what I have done wrong?

Upvotes: 4

Views: 15129

Answers (2)

BalusC
BalusC

Reputation: 1108992

I have no idea what a properties file is.

A Java .properties file is just a configuration file with key=value pairs in each line, like a Windows (actually, C++/C#) .ini file.

See also:


Can I create a properties file from text editor, and have the path set in the catalina.properties file in the Tomcat/conf folder?

It boils down to that the file has to be placed in the Java classpath. In case of a Java web application, you can put it in one of the existing paths which is covered by the classpath, such as /WEB-INF/classes (in an IDE, placing the file in root folder of Java source should make it to end up in /WEB-INF/classes). You can of course also put it somewhere else and add its root path to the classpath, as you're trying to do with shared.loader of Tomcat.


So anyway I did the following:

# starting with file:
shared.loader= /home/shaunkoo/NetBeansProjects/dao.properties

This is not correct, it has to point to a folder which represents the classpath root, or to an individual JAR file. In your case, it should be:

shared.loader= /home/shaunkoo/NetBeansProjects

This way the content of the above folder becomes part of the classpath.


and load the file in via

private static final String PROPERTIES_FILE = "/NetBeansProjects/dao.properties";

This is not correct, the path for context class loader cannot start with / and should not point to a folder which is not inside the classpath. You've specified the /home/shaunkoo/NetBeansProjects to be part of the classpath, so any files which are inside the folder are accessible by the class loader, not the folder itself. In your case, it should be:

private static final String PROPERTIES_FILE = "dao.properties";

Upvotes: 7

Mark Rotteveel
Mark Rotteveel

Reputation: 109087

getResourceAsStream resolves against the classpath, not your filesystem path. So you will need to put the file in your source folder (eg in /src/resources/dao.properties

And then use something like getResourceAsStream("/resources/dao.properties")

Upvotes: 0

Related Questions