First of all I would like to say am new to h2 database. I need to execute a sql script file in h2 database. I have a script file test.sql and I want to execute this in h2 database. Is it possible?
Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .
To install and work with H2 database, find the steps. 1. Go to official website link. Download and install in your computer. 2. Open H2 Console (Command Line) and access URL http://localhost:8082 in browser. Select server mode and in JDBC URL we can change 'test' database name as we want. In our example we will change database name as 'mydb'.
H2 database is the open source Java SQL database. In our example we will create a batch application which will read data from a CSV file and write into H2 database. In our Spring Batch application, we will use FlatFileItemReader to create reader and JdbcBatchItemWriter to create writer.
The logger name is h2database . If it does not work, check the file <database>.trace.db for error messages. If the database files are read-only, then the database is read-only as well. It is not possible to create new tables, add or modify data in this database.
The tool is available from within the H2 Console in the tools section, or you can run it from the command line. The following command line will encrypt the database test in the user home directory with the file password filepwd and the encryption algorithm AES:
You can use the RUNSCRIPT SQL statement:
RUNSCRIPT FROM 'test.sql'
or you can use the RunScript standalone / command line tool:
java -cp h2*.jar org.h2.tools.RunScript -url jdbc:h2:~/test -script test.sql
You can also use the RunScript tool within an application:
RunScript.execute(conn, new FileReader("test.sql"));
If you are using spring-boot and spring-test with H2 it will automatically look for schema.sql
and data.sql
in your class path and attempt to run these. So if you put them in src/test/resources
they should be picked up and run automatically
In addition you can specify the data files you want to run with properties. For example adding a property to yourapplication.properties
like
spring.datasource.data=classpath:users.sql, classpath:books.sql, classpath:reviews.sql
will configure spring to run those three sql files instead of running data.sql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With