package jdbc.examples;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Properties;public class PropertiesDemo { private static Connection conn; private static Statement st; private static ResultSet rs; public static void main(String[] args) throws Exception{ Properties p = new Properties(); FileInputStream fis = new FileInputStream("db.properties"); p.load(fis); String driver = (String)p.getProperty("driver"); String url = (String)p.getProperty("url"); String user = (String)p.getProperty("user"); String pwd = (String)p.getProperty("pwd"); Class.forName(driver); conn = DriverManager.getConnection(url, user, pwd); st = conn.createStatement(); rs = st.executeQuery("select ename, sal, deptno from emp"); while(rs.next()) { System.out.println(rs.getString(1)+" "+rs.getDouble(2)+" "+rs.getInt(3)); } rs.close(); st.close(); conn.close(); }}輸出:Exception in thread "main" java.io.FileNotFoundException: db.properties (The system cannot find the file specified) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(Unknown Source) at java.base/java.io.FileInputStream.<init>(Unknown Source) at j
2 回答

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
就像@Carlos和@PM77-1提到的那樣,答案是直截了當的“找不到文件”,用更簡單的術語來說,
FileInputStream fis = new FileInputStream(“db.properties”);
上面的行在 java 代碼的同一文件夾中搜索文件 db.properties,但未找到它。
解決方案:您可能需要移動屬性文件,或者您必須提及絕對/相對路徑
FileInputStream fis = new FileInputStream(“c:\myFile\db.properties”);

臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
您嘗試打開的文件不存在。下面的一行是導致問題的原因。最有可能的是,該文件位于另一個文件夾中或具有不同的名稱,因此我會先檢查一下。
FileInputStream fis = new FileInputStream("db.properties");
添加回答
舉報
0/150
提交
取消