Exporting Data from Postgres with Copy command in SpringBoot - java

I'm trying to do an export of a PostgreSQL table from Spring-boot application. I've a logic to select all the records and map it to a CSV. The application presently retrieves all the data and uses CSV libraries to format/export it. There is also direct command from PostgreSQL to export the data (COPY command), without using any APIs. Is it a way to have this done from application.
Added Queries from Spring-boot and tried executing the copy operation, where the spring is not allowing the command execution.
CopyManager from Postgres is recommended with Spring-boot?
Is there a way i can get the data directly from DB without the data getting retrieved by the application?

COPY will create a file on the database server, so unless that's really what you want, don't use COPY.
If you want the data in a file on the application machine, use a SELECT statement, and format the output as CSV in the Java code, preferably using a CSV library.

Related

Write sql insert and update queries while saving or updating in h2 database programatically

I am trying to create a local project for my learning purpose in which I am using h2 as in-memory database.
I am using spring-jpa for my project.
I was wondering is there any way we can take dump of data or writing logs (in this case queries) in a file.
Why do I need this?
so, as h2 is an in-memory database, whenever project ends it deletes all the data.
what I want to get all the data when I re-run the process again.
Tried on searching on internet couldn't find anything.
Instead of using H2, which is solely in-memory, consider using SQLite, which can create a file and will be left behind after the application is terminated.
H2 is not an in-memory database system, H2 supports both persistent and in-memory databases. If you need to preserve your data, use file-based database with jdbc:h2:./somename URL, where ./somename is relative (must starts with ./) or absolute path to the database file without extension.
https://www.h2database.com/html/features.html#database_url
Of course, you can export your data into external file even if you use an in-memory database with SCRIPT TO 'filename.sql' command of H2, but there are no good reasons to use that functionality together with in-memory database instead of persistent one. Such dumps are usually used for migration.

How to create a temporary database with postgresql initdb?

How do I create a temporary database for PostgreSQL using initdb? Also, how do I populate it with dummy data and how do I delete it after usage?
I have an entire schema of the database. I don't want to create temporary tables one by one. Using initdb, I would like to be able to give the schema and get a database with the tables, etcetera created.
This is for running integration tests of a spring boot application. I use mybatis to access the database.
Can I use any other tool like arquillian?
There's a workaround this, just change the data directory to a RAM file system, like this once you restart your computer, the data is gone. better you won't change anything in your scripts.

JSF, website first setup and external configuration files

I have a website written in JSF backed by MySQL database running on Tomcat 7. Now there is only one missing part - project first setup/installation. I want my war when deployed for the first time to offer you installation/first time setup with following steps:
Setup database - enter mysql parameters needed to successfully connect to mySQL server.
Write those parameters into some external file for further use (of course encrypted).
Install database - take a file with SQL inside that creates all the tables in database.
Create first user etc.
Delete installation files.
Similar steps are used in PHP Content Management systems like Drupal. I know perfectly how to work with files in Java. I also know, that I can't change content inside a jar once it's deployed and running, so I have to put my files with SQL and database parameters somewhere else.
My questions are
Where can I put these configuration files to make them readable ? And how ?
Is there another way to achieve this goal ? What is commonly used by Java developers ?
Thank you for your answers.
You can use JPA(Java persistent API) and put all this configuration on persistance.xml and set the schema generation to create Table also the things related to role and user is dependent to application server.
JPA use ORM(object relational mapping) to map between you objects (entity) and database tables

Can I use JPQL to query a MySQL database which was populated by loading CSV files?

This is something of a noob question, so please bear with me.
I'm building a Java web app which is deployed on JBoss. Part of the functionality is populating a MySQL DB with data from an Excel spreadsheet. This can be achieved in 2 ways:
Using JExcel / Apache POI to parse the spreadsheet data and creating Entity "beans" which are then persisted to the DB.
Using scripts to convert the spreadsheet to csv files and then load the csv files into the DB.
My question is: If I choose the scripting / csv route, can I still use JPQL to query the DB or will I have to resort to native SQL queries in the Java code?
JPQL can be used to query table independently from method that was used to populate table. Data stored to table is not aware of with which method it was inserted.
JPA is not notified about changes made to data via script, but in typical use case with no additional caches and transaction-scoped PersistenceContext that is not the issue, because query will hit the database and deliver fresh data.

MongoDB (Java) - How to run query saved in javascript file?

So I have a lot of .js files that I have used to query MongoDB from the Command Line Interface before, but now I want to be able to run those same queries through Java (I am using Java to back a web interface that relies on the information from the query). How can I use those JavaScript queries from the Java driver and return some data that I can work with (the end game is to format the result into HTML, if that helps).
If you need to executye your js files during buildtime, you can use maven-mongodb-plugin. This plugin uses db.eval()...
using Java IO to read your js files, filter the queries , execute the queries.
This not possible in an efficient way in current JAVA driver.
Have a look here :
mongodb java driver - raw command?

Categories

Resources