我正在運行Mac OS X並且代碼正常工作。我的隊友正在運行Windows 7,並且他得到了錯誤(即使他的系統上存在目錄結構和文件)。有任何想法嗎?我已經花了幾個小時對此無法弄清楚。謝謝。如何解決Java錯誤「系統找不到指定的路徑」?
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; // Not used
import java.sql.ResultSet; // Not used
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList; // Not used
import java.util.Scanner; // Not used
public class CreateDB {
public CreateDB() {
}
public void createDB() {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = ""; // Not used (yet?)
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; // Tell user userName is "root". OR/TODO: Change to something more secure?
String password = ""; // Tell user password is "". Or/TODO: Change to something more secure?
String line;
String sqlDump = null;
String [] sqlDumpArray;
String fileSeperator = System.getProperty("file.separator");
System.out.println("fileSeperator is: " + fileSeperator); // Testing
String path = System.getProperty("user.dir");
System.out.println("path is: " + path); // Testing
String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
System.out.println("pathToFile is: " + pathToFile); // Testing.
String fileName = "create_db_and_tables.sql"; // NOTE: Rename file, if necessary. Make sure it's in right place.
System.out.println("pathToFile + fileName is: " + pathToFile + fileName); // Testing.
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected to the database.\n"); // Testing
BufferedReader reader = new BufferedReader(new FileReader(pathToFile + fileName));
line = reader.readLine();
while (line != null) {
sqlDump += line + "\n";
line = reader.readLine();
}
System.out.println(sqlDump); // Testing
sqlDumpArray = sqlDump.split(";");
System.out.println("sqlDumpArray size is " + sqlDumpArray.length); // Testing
for (int i = 0; i < sqlDumpArray.length; i++) {
System.out.println("\nsqlDumpArray[" + i + "] is: " + sqlDumpArray[i]); // Testing
}
for (int j = 1; j < sqlDumpArray.length -1; j++) {
try {
Statement statement = conn.createStatement();
statement.addBatch(sqlDumpArray[j]);
statement.executeBatch(); // Could refactor or us another method
}
catch (SQLException e) {
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("ClassNotFoundException: " + e.getMessage());
}
catch (SQLException e) {
System.err.println("SQLException: " + e.getMessage());
}
catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
finally {
try {
// conn.commit(); // Not necessary
conn.close();
System.out.println("Successfully disconnected from database. Yeah!");
}
catch (SQLException e) {
System.err.println("SQLException in 'finally' block: " + e.getMessage());
}
}
}
public static void main(String[] args) {
CreateDB create = new CreateDB();
create.createDB();
}
}
當試圖執行數據庫創建過程時,Windows上的用戶已收到錯誤。
String fileSeperator = System.getProperty("file.separator");
System.out.println("fileSeperator is: " + fileSeperator);
String path = System.getProperty("user.dir");
System.out.println("path is: " + path);
String pathToFile = (path + fileSeperator + "src" + fileSeperator + "files" + fileSeperator);
System.out.println("pathToFile is: " + pathToFile);
String fileName = "create_db_and_tables.sql";
System.out.println("pathToFile + fileName is: " + pathToFile + fileName);
這是什麼一個例子:即使在路徑和文件似乎是正確的,比如什麼是下面的System.out.println語句文件中返回(以下高亮顯示)時出現此錯誤在Windows 7計算機上的語句返回:
Exception: C:\Users\USER\workspace\mrbs2011a\src\files\create_db_and_tables.sql (The system cannot find the path specified)
這被打印出來,即使用戶已經位於「SRC /文件」文件夾中的文件「create_db_and_tables.sql」。
什麼行有錯誤? Windows 7上的Mac OS X與填充文件路徑的外觀如何? – Romain
請發送完整例外。 – korifey
你在哪裏得到例外? stacktrace請 – mprabhat