Mysql 如何从servlet中的.property文件中读取值?

Mysql 如何从servlet中的.property文件中读取值?,mysql,servlets,Mysql,Servlets,我正在尝试使用config.properties文件中的值连接到数据库。每次它给文件找不到异常。同样的代码也适用于核心java Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("prop/config.properties"); // load a properties file pr

我正在尝试使用config.properties文件中的值连接到数据库。每次它给文件找不到异常。同样的代码也适用于核心java

Properties prop = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("prop/config.properties");

        // load a properties file
        prop.load(input);


        String ipaddress=prop.getProperty("com.mysql.ipaddress");
        String portno=prop.getProperty("com.mysql.portno");
        String dbname=prop.getProperty("com.mysql.dbname");
        String user=prop.getProperty("com.mysql.user");
        String password=prop.getProperty("com.mysql.password");

        // get the property value and print it out

        System.out.println(ipaddress);
        System.out.println(portno);
        System.out.println(dbname);
        System.out.println(user);
        System.out.println(password);

         try {Class.forName("com.mysql.jdbc.Driver");} 
           catch (ClassNotFoundException e) {e.printStackTrace();}
            try 
            {

                Connection connection2 = DriverManager.getConnection("jdbc:mysql://"+ipaddress+":"+portno+"/"+dbname,user,password);

            Statement stmt1=connection2.createStatement();
            ResultSet resultset1=stmt1.executeQuery("SELECT * FROM main_countt order by 1 desc");

            while(resultset1.next()) {

                System.out.println("Hello");
                System.out.print("<option id='"+resultset1.getString(1)+"' >"+resultset1.getString(2)+"</option>");        
                        }

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
Properties prop=新属性();
InputStream输入=null;
试一试{
输入=新文件输入流(“prop/config.properties”);
//加载属性文件
道具载荷(输入);
字符串ipaddress=prop.getProperty(“com.mysql.ipaddress”);
字符串portno=prop.getProperty(“com.mysql.portno”);
字符串dbname=prop.getProperty(“com.mysql.dbname”);
字符串user=prop.getProperty(“com.mysql.user”);
字符串password=prop.getProperty(“com.mysql.password”);
//获取属性值并将其打印出来
System.out.println(ipaddress);
系统输出打印项次(端口号);
System.out.println(dbname);
System.out.println(用户);
System.out.println(密码);
试试{Class.forName(“com.mysql.jdbc.Driver”);}
catch(ClassNotFoundException e){e.printStackTrace();}
尝试
{
ConnectionConnection2=DriverManager.getConnection(“jdbc:mysql://“+ipaddress+”:“+portno+”/“+dbname、用户、密码);
语句stmt1=connection2.createStatement();
ResultSet resultset1=stmt1.executeQuery(“按1描述从主计数顺序中选择*);
while(resultset1.next()){
System.out.println(“你好”);
System.out.print(“+resultset1.getString(2)+”);
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}捕获(IOEX异常){
例如printStackTrace();
}最后{
如果(输入!=null){
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
我得到的例外情况:

java.io.FileNotFoundException: prop\config.properties (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at Test.servlet1.service(servlet1.java:44)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
java.io.FileNotFoundException:prop\config.properties(系统找不到指定的路径)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(未知源)
位于java.io.FileInputStream。(未知源)
位于Test.servlet1.service(servlet1.java:44)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
位于org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
位于org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
位于org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
位于org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
位于org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
位于org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
位于org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
位于org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源)
位于java.lang.Thread.run(未知源)

您似乎试图访问错误的路径。尝试放置完整路径(绝对路径):


同样的代码适用于核心java,也适用于绝对路径。我想要的是读取servlet中的属性文件值。我不能使用绝对路径,因为我将稍后在服务器上部署它。您的文件的绝对路径是什么?谢谢您的重播。我从上面提供的链接中得到了答案。我使用了下面的代码及其工作原理。加载(Thread.currentThread().getContextClassLoader().getResourceAsStream(“prop/config.properties”);文件位置:WEB-INF/classes/prop/config.properties我很高兴您解决了问题。请将您的解决方案作为答案,让其他人看到问题并知道问题已经解决。感谢您发布您的解决方案!
c:/.../prop/conf...