I have a H2 h2.mv.db database physical file in "users" directory of my machine. I want my application using Spring Boot to use H2 in memory database, but when the app start I want the in memory database to import the h2.mv.db file. I know I can export a SQL script then execute it manually using H2 web application, but can this be achieved programatically?
You can configure the spring.datasource.url to point to the file.
Another approach to this problem is to export the SQL script as you said and put it inside a file called src/main/resource/data.sql.
Spring Boot automatically picks up the data.sql file and run it against the H2 database during the application startup.
#persist the data
spring.datasource.url=jdbc:h2:file:/data/sampledata
spring.datasource.url=jdbc:h2:C:/data/sampledata
Source: https://www.javatpoint.com/spring-boot-h2-database
Related
I'm currently trying to import an Oracle DB .dmp (dump) file into my Oracle DB using DBeaver but have trouble doing so.
The Oracle DB in question is running in a docker container. I successfully connected to this Oracle database with DBeaver, and can thus browse the database using DBeaver.
Currently however, the DB is empty. That's where the .dmp file comes in.
I want to import this .dmp file into my database, under a certain schema but I cannot seem to do this. The dump file looks something like this: 'export.dmp' and is around 16MB big.
I'd like to import the data from the .dmp file to be able to browse the data to get familiar with it, as similar data will be stored in our own database.
I looked online but was unable to get an answer that works for me.
I tried using DBeaver but I don't seem to have the option to import or restore a DB via a .dmp file. At best, DBeaver proposes to import data using a .CSV file. I also downloaded the Oracle tool SQLDeveloper, but I can't manage to connect to my database in the docker container.
Online there is also talk of an import / export tool that supposedly can create these .dmp files and import them, but I'm unsure how to get this tool and whether that is the way to do it.
If so, I still don't understand how I can get to browse the data in DBeaver.
How can I import and browse the data from the .dmp file in my Oracle DB using DBeaver?
How to find Oracle datapump dir
presumably set to /u01/app/oracle/admin/<mydatabase>/dpdump on your system
How to copy files from host to docker container
docker cp export.dmp container_id:/u01/app/oracle/admin/<mydatabase>/dpdump/export.dmp
How do I get into a Docker container's shell
docker exec -it <mycontainer> bash
How to import an Oracle database from dmp file
If it was exported using expdp, then start the import with impdp:
impdp <username>/<password> dumpfile=export.dmp full=y
It will output the log file in the same default DATA_PUMP_DIR directory in the container.
oracle has two utilities IMPORT and IMPDP to import dumps , with IMPORT you can't use database directories and you have to specify the location . The IMPDP on other hand require database directory .
having said that you can't import oracle export dumps using dbeaver , you have to use IMPORT or IMPDP utility from OS.
I have spring boot application that need to be deployed on PCF. I want to use H2 database for local testing but after deployed in PCF I will be using SQL server. I have a schema that need to be used for each database. So, I have two schema.sql file one for H2 and another for SQL server. How can I tell spring for local profile schema-H2.sql need to be used and for profile cloud schema-sqlserver.sql need to be used.
You can set the spring.datasource.platform to differentiate the schema and data sql file.
eg,
spring.datasource.platform=h2
then the file name should be data-h2.sql and schema-h2.sql
Make sure you set spring.datasource.initialization-mode=always
I have created a spring boot project in eclipse with a H2 embedded database and JPA. Inside my resource folder I'm using import.sql to populate the tables created in my entity package.The spring web application and database runs fine as I can check it but I want to know if it is possible to check this database in Intelij DataGrip like I can when I do this:
localhost:8080/h2-console
In my application.properties in my spring project I have this:
spring.datasource.url=jdbc:h2:file:./database/suppliersDB;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
In my Intelij Datagrip I add a data source H2 and give it the url and test connection and it says ok but doesn't show any of the tables and when I run a query in the DLL it says table doesn't exist
And I know I should probably shouldn't be using Intelij IDE since my project is in eclipse but it is just an experiment and it is frustrating me I can't get it working in Datagrip.
Also the embedded database file stored by eclipse is an .mv.db file which maybe the problem i'm not sure!
Any help would be much appreciated!
You cannot do it because the database you created is in-memory.
The H2 database used in the Java Todo List tutorial is the following:
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
How do I modify the configuration file to use a persistent database as opposed to an in-memory version. Do I need to setup an entirely separate DB or can I modify the db.default.url property?
I'm using Play! 2.0.3.
I found the solution.
To create a file database, modify the following:
From
db.default.url="jdbc:h2:mem:play"
To
db.default.url="jdbc:h2:file:data/db"
Where data/db is broken down into:
data/ The folder location of the database files relative to your project root.
db The name of your database files.
I export my application to .apk file, sign it and then install it. But when I run my app, it displays an error because there's no data in my database.
The database was created as a new one when I installed the application, so all the data were lost!
How can I include database data when exporting an Android application?
Option #1: Package the database as a raw resource or asset, and use streams to copy that database to its proper position when the app is first run.
Option #2: Package SQL statements to populate the database as a raw resource, and execute those statements in your SQLiteOpenHelper's onCreate() method.
Option #3: Put the smarts straight in your Java code to populate your database in your SQLiteOpenHelper's onCreate() method.
Option #4: Download the database on first run and copy it to its proper position.
And so on.