【发布时间】:2011-12-08 13:20:51
【问题描述】:
我正在运行 Mac OS X 并且代码有效。我的队友正在运行 Windows 7,他得到了错误(即使目录结构和文件存在于他的系统上)。有任何想法吗?我已经花了几个小时在这上面,但无法弄清楚。谢谢。
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 上的用户在尝试执行数据库创建过程时收到错误消息。即使路径和文件看起来正确,也会发生此错误,例如文件中以下System.out.println 语句返回的内容(在下面突出显示):
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);
这是该语句在 Windows 7 计算机上返回的示例:
Exception: C:\Users\USER\workspace\mrbs2011a\src\files\create_db_and_tables.sql (The system cannot find the path specified)
即使 USER 的文件 create_db_and_tables.sql 位于 src/files 文件夹中,它也会被打印出来。
【问题讨论】:
-
哪几行有错误? Mac OS X 与 Windows 7 上的填充文件路径是什么样的?
-
你在哪里得到异常?请堆栈跟踪
-
嗨,到目前为止,感谢 cmets。编辑了问题,添加了向我报告的异常(见问题底部)。我添加了 e.printStackTrace();到所有的 catch 块,并要求我的队友再次运行它,并将在他的控制台中打印的所有内容发送给我。他现在正在看医生,所以我可能需要一点时间才能回复详细信息。同时,如果有人在使用 Windows 7 并且可以抽出几分钟时间,您可以尝试一下,看看您会得到什么吗?谢谢。
-
在 Mac OS X 上,我从适用的 System.out.println 语句中得到这些结果: fileSeperator is: / path is: /Users/chris/Documents/workspace/mrbs2011a pathToFile is: /Users/ chris/Documents/workspace/mrbs2011a/src/files/ pathToFile + fileName 是: /Users/chris/Documents/workspace/mrbs2011a/src/files/create_db_and_tables.sql 在 Windows 7 上,这是我迄今为止向我报告的内容(如果您使用适用的代码行,可以在您的系统上对其进行测试):路径为:C:\Users\USER\workspace\mrbs2011a pathToFile 为:C:\Users\USER\workspace\mrbs2011a\src\files\
标签: java exception file-io filesystems