Execute SQL Scripts in a Spring Boot Commandline Application - spring-boot

I have a Spring Boot command line application packaged as a Jar which will take database details from the user. Using those details, I have to connect to the database and execute .sql file against that database
For example:
Seed the database programmatically
Execute scripts against the database
Can anyone help me with an option or link as to how can I do that?

Here's a guide to getting your parameters into your Spring Boot app:
https://memorynotfound.com/spring-boot-passing-command-line-arguments-example/
Here's a guide to running logic on startup:
https://www.baeldung.com/running-setup-logic-on-startup-in-spring

Related

Spring boot r2dbc and jdbc Liquibase does not update on application start

I want my application to use r2dbc drive when running and jdbc to handle the database with Liquibase. I have this as a reference. It is possible by adding the correct configuration inside application.properties and build.gradle.kts to achieve that. Update and everything is working but not automatically. Do you have any suggestions?Spring's reference
You have two options to use db migrations like Liquibase in a R2bc application.
Till now Liquibase did not support R2dbc, and by default R2dbc and Jdbc can not be activated in a single Spring boot application. In a reactive Spring Boot application, although you added a Jdbc starter, it is not activated by default.
But you can setup liquibase in a R2dbc application, and configure Liquibase Maven/Gradle plugin to migrate the database manually by Gradle tasks/Maven goals. See my example: liquibase-maven-r2dbc, make sure the db is running, and execute mvn liquibase:update in the project root.
See the official guide of Liquibase Maven PLugin, https://docs.liquibase.com/tools-integrations/maven/workflows/using-liquibase-maven-plugin-and-springboot.html
Use R2dbc Migrate directly, it is similar to Flyway but working with 2rdbc. See my example: r2dbc-migrate.

Spring Batch does not run when importing H2 SQL scripts

I am trying to run a Spring Batch application to create batch jobs.
My application.properties as shown below:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=admin
spring.datasource.password=p#ssw0rd
spring.datasource.schema=schema-hsqldb.sql
The error I am getting is shown below:
Caused by: org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'class path resource [schema-hsqldb.sql]' is invalid: The specified resource does not exist.
As far as I know, the schema-hsqldb.sql exists in classpath /org/springframework/batch/core/schema-hsqldb.sql.
The Spring boot application works when I commented out spring.datasource.schema=schema-hsqldb.sql. Do I have to manually import the SQL scripts? If so, how do I do that?
Your issue is that you are using the H2 driver with the schema of HSQL. H2 and HSQL are different products. You need to use the schema of H2 as well:
spring.datasource.schema=/org/springframework/batch/core/schema-h2.sql
That said, you don't really need to configure the datasource for embedded databases with Spring Boot, the datasource will be automatically configured. Here is an excerpt from the Embedded Database Support section of the reference docs:
Spring Boot can auto-configure embedded H2, HSQL, and Derby databases. You need not provide any connection URLs. You need only include a build dependency to the embedded database that you want to use.

spring boot jbpm project load .bpmn file and start proecess

Currently i am trying to Run JBPM with Spring boot with tomcat(Not jboss) using .bpmn file.
I am facing many issues. Could you please help me regarding the below points.
1- How load .BPMN file in spring boot.
2- what are the maven dependency required.
3- how start start process,human task etc.
It will be really helpful if any one able to share running code including below points
R

Defining Database dynamically in spring boot maven based war application

My Spring boot application won't know the database to be connected in prior, once before application deployment, the user will select the database to be connected, and places the jar in the server webinf(or probably some other repository path), and changes the externalized properties file, so that application connects to the database, I was trying giving the database dependency scope as provided, but getting class not found. What is the preferred approach for solving the issue?
You can run your Spring Boot bootJar like this:
java -cp your-jdbc-driver.jar -jar your-boot-jar.jar --spring.datasource.url=your:jdbc:url

Spring Boot Batch with Oracle as JobRepository

I am searching for some good example of Spring Boot Batch where Oracle DB is used as Job Repository with Java Config.I have been able to run the batch with embedded DB.But while trying to use the Spring Batch Admin, am facing problems.
Thanks in Advance.
This is a good example of Spring Boot with Spring Batch Admin.
https://github.com/codecentric/spring-batch-admin-spring-boot
All you need to do is replace batch-hsql.properties with batch-oracle.properties and add
-Denvironment=oracle property to your server.

Resources