JDBC Property File

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

I do not know what a properties file is. Can I create a properties file from a text editor and set the path in the catalina.properties file in the Tomcat / conf folder? or 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 in shared.loader = to catalina.properties file?

So anyway I did the following:

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

and upload the file 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 get an error message indicating that /NetBeansProjects/dao.properties is missing from the class path. Any clues what I did wrong?

+4
source share
2 answers

I do not know what a properties file is.

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

See also:


Is it possible to create a properties file from a text editor and set the path in the catalina.properties file in the Tomcat / conf folder?

It comes down to the fact that the file must be placed in the Java classpath. In the case of a Java web application, you can put it in one of the existing paths that covers the class path, for example /WEB-INF/classes (in the IDE, placing the file in the root folder of the Java source should make it end in /WEB-INF/classes ). You can, of course, also put it in another place and add your root path to the class path, as you are trying to do with shared.loader Tomcat.


So anyway I did the following:

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

This is not true, it should point to a folder representing the root of the class path, or a separate JAR file. In your case, it should be:

 shared.loader= /home/shaunkoo/NetBeansProjects 

Thus, the contents of the specified folder becomes part of the class path.


and upload the file through

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

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

 private static final String PROPERTIES_FILE = "dao.properties"; 
+7
source

getResourceAsStream resolves the class path, not the file system path. Therefore, you will need to place the file in the source folder (for example, in /src/resources/dao.properties

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

0
source

Source: https://habr.com/ru/post/1382471/


All Articles