How to access CSV file in WAR using CSREREAD () H2 CS function / request?

I am trying to read a CSV file from 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 need / need to specify an absolute path (it will be deployed on a Linux / Tomcat server, which I will not have access to), and I'm not sure about the need for a file protocol ('jar: file', classpath: etc. .d.).

The file is in the section "** / WEB-INF / classes / csvfile.csv"

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

thanks

Rich

+4
java sql database csv h2
source share
2 answers

Perhaps you could try to build the query dynamically by first returning the full path using ServletContext.getRealPath("/WEB-INF/classes/csvfile.csv") .

+2
source share

H2 currently does not support downloading files from the classpath. However, you should get the resource URL using:

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

And then you can use this URL (in my case, this is the URL starting with "file:") as follows:

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

(I have not tested this in a web application)

+1
source share

All Articles