简体   繁体   English

如何将.jar文件中的数据加载到Mysql表中?

[英]How can I load data from file within .jar to Mysql table?

I want to load data from .txt file (which is in project folder) to mySQL table. 我想将数据从.txt文件(位于项目文件夹中)加载到mySQL表中。

String path = getClass().getResource("/DataEmployees.txt").getPath();
String query = "LOAD DATA LOCAL INFILE ? INTO TABLE employees LINES TERMINATED BY '\r\n' STARTING BY 'xxx';";
try( Connection connection = Main.dataSource.getConnection() ){
    try( PreparedStatement preparedStatement = connection.prepareStatement(query) ){
        preparedStatement.setString(1, path);
        preparedStatement.executeUpdate();
    }
}catch(Exception exception){
    ExceptionDialog exceptionDialog = new ExceptionDialog("Couldn't load default sample data",exception);
    exceptionDialog.showAndWait();
};

But I get an error: 但是我得到一个错误:

java.sql.SQLException: Unable to open file '/C:/Users/Tomek/EclipseWorkspace/Pizza%20Service/bin/DataEmployees.txt'for 'LOAD DATA LOCAL INFILE' command.Due to underlying IOException: 

** BEGIN NESTED EXCEPTION ** 

java.io.FileNotFoundException
MESSAGE: C:\Users\Tomek\EclipseWorkspace\Pizza%20Service\bin\DataEmployees.txt (The system cannot find the path specified)

STACKTRACE:

java.io.FileNotFoundException: C:\Users\Tomek\EclipseWorkspace\Pizza%20Service\bin\DataEmployees.txt (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.mysql.jdbc.MysqlIO.sendFileToServer(MysqlIO.java:3674)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2995)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2245)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2638)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
    at 

DataEmployees is in another source folder. DataEmployees在另一个源文件夹中。

在此处输入图片说明

I don't know what is wrong here and how can I fix it. 我不知道这里出了什么问题以及如何解决。 It works when I use absolute path: String path = "C:/Users/Tomek/EclipseWorkspace/Pizza Service/files/DataEmployees.txt"; 当我使用绝对路径时,它可以工作: String path = "C:/Users/Tomek/EclipseWorkspace/Pizza Service/files/DataEmployees.txt";

EDIT 编辑

String path="";
try {
    path = getClass().getResource("/DataEmployees.txt").toURI().getPath();
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
String query = "LOAD DATA LOCAL INFILE ? INTO TABLE employees LINES TERMINATED BY '\r\n' STARTING BY 'xxx';";

this works but within the eclipse. 这可行,但在日食范围内。 If I export jar file to desktop then I get exception: 如果我将jar文件导出到桌面,则会出现异常:

java.sql.SQLException: Unable to open file 'null'for 'LOAD DATA LOCAL INFILE' command.Due to underlying IOException: 

** BEGIN NESTED EXCEPTION ** 

java.io.FileNotFoundException
MESSAGE: null (The system cannot find the file specified)

STACKTRACE:

java.io.FileNotFoundException: null (The system cannot find the file specified)

If you want to read that file from inside your jar use: 如果要从jar内部读取该文件,请使用:

InputStream input = getClass().getResourceAsStream("/path/to/file");

The path starts with "/", but that is not the path in your file-system, but in your classpath (in-side jar). 路径以“ /”开头,但这不是文件系统中的路径,而是类路径(内部jar)中的路径。 So if your file is called data.txt and is in files folder at the root of jar file you should use getResourceAsStream("/files/data.txt") to read it as stream. 因此,如果您的文件名为data.txt且位于jar文件根目录下的files文件夹中,则应使用getResourceAsStream("/files/data.txt")将其作为流读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM