我正在尝试使用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();
}
}
}
我得到的例外情况:
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)
答案 0 :(得分:-1)
您似乎正在尝试访问错误的路径。尝试输入完整路径(绝对路径):
c:/.../prop/conf...