开发者

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

开发者 https://www.devze.com 2023-02-06 05:37 出处:网络
I\'m trying to read开发者_如何学编程 a CSV file from within a web application (Tomcat 5.5.x) and all I get is \'FileNotFoundExceptions\'s:

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


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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消