Download the CSV file located in the classpath for the H2 database

For testing purposes, I want to create and populate some tables using SQL scripts, as well as CSV files.

So, I created a SQL script, like this one:

CREATE TABLE T_FOO ( ... ) as select * from CSVREAD('classpath:/foo.csv'); 

The file foo.csv exists and is located in src/test/resources .

When this script runs on Eclipse (where src/test/resources is defined as the source directory and therefore included in the classpath), I get the following error:

 Caused by: java.io.FileNotFoundException: resource /foo.csv at org.h2.store.fs.FileSystemDisk.openFileInputStream(FileSystemDisk.java:388) at org.h2.util.IOUtils.openFileInputStream(IOUtils.java:708) at org.h2.tools.Csv.initRead(Csv.java:317) at org.h2.tools.Csv.readResultSet(Csv.java:217) at org.h2.tools.Csv.read(Csv.java:193) ... 49 more 

What did I do wrong? How to use classpath: protocol classpath: to load CSV file?

If I put the full path to the file (for example ... CSVREAD('C:\my-project\src\test\resources\foo.csv'); ), it will work. But this is not what I want to do :)

Please note that I am using the latest version of H2 ( 1.3.153 ) as I wanted to use the classpath: protocol to upload my file .

+8
java testing h2 in-memory-database
source share
1 answer

Even if the official docs give an example of CSVREAD('classpath:/org/acme/data/address.csv') Sean Patrick Floyd suggested removing the leading slash, i.e. having:

 CREATE TABLE T_FOO ( ... ) as select * from CSVREAD('classpath:foo.csv'); 

and it works !

+10
source share

All Articles