How to access CSV file within WAR using H2's CSVREAD() function/query?

筅森魡賤 提交于 2019-12-10 17:33:13

问题


I'm trying to read a CSV file from within a web application (Tomcat 5.5.x) and all I get is 'FileNotFoundExceptions's:

dbStatement.executeQuery("SELECT * FROM CSVREAD('csvfile.csv');");

I don't think I can/need to specify an absolute path (it will be deployed into a Linux/Tomcat server I won't have access to) and am unsure of the file protocol necessary ('jar:file', classpath: etc).

The file is under "**/WEB-INF/classes/csvfile.csv"

Any ideas on the structure of the path I need to pass to CSVREAD()?

Thanks

Rich


回答1:


Maybe you could try to dynamically build the query, first retrieving the full path with ServletContext.getRealPath("/WEB-INF/classes/csvfile.csv").




回答2:


H2 currently doesn't support loading files from the classpath. However, you should be able to get the URL of the resource using:

String url = getClass().getClassLoader().getResource("csvfile.csv").toString();

And then you can use this URL (in my case it's an URL starting with "file:") as follows:

dbStatement.executeQuery("SELECT * FROM CSVREAD('" + url + "');");

(I didn't test this in a web app however)



来源:https://stackoverflow.com/questions/4784859/how-to-access-csv-file-within-war-using-h2s-csvread-function-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!