Using HSQLDB as a portable database with Spring, Hibernate - java

I am newbie in hsqldb.
In the project I am using Spring 4, Hibernate 5 and HSQLDB.
I am having some specific task and I am trying to use HSQLDB as a portable database, which can be transferred to a flash drive or another computer.
I already have an sql-script with all tables and basic-needed data.
I have four questions that are haunting me.
(I'm sorry in advance if these questions are very stupid):
I need to make the script run at the first launch of the program, and in the other launсhes it must to check if database already exist and (if it already exists)only update data in it. (the program would be used in many computers and the database must be created after the first launch).
How can I do this? Is it possible? Can you give some basic advice or example about how I can to do that?
I am trying to find some information about saving all database info in some file in the file-system. Can you give me please some valid examples about saving hsqldb data in file and about using this file after another launch.
Can I place this file in my project.jar file and to work with all data from it update it e.t.c. ?
What is the best practice to make my database portable(for specific tasks) and where should I keep it? In file, in my project jar.e.t.c.?
Thanks in advance for your answers!

For data storage, you use a file: database. The JDBC connection URL is in the form jdbc:hsqldb:file:<file path>. HSQLDB will save all the data to file.
After connecting to the database you execute the SQL statements in your script one by one. If the tables already exist, the CREATE TABLE statements throw an error. This shows you don't have to execute them.
Because it's up to you when to keep the existing data and when to update it based on the existing contents of the database, you execute some SQL statements to decide. There is no automatic way to do this.
You can put an HSQLDB database in a jar but it cannot be updated. Jars are read-only.
The databases are fully portable. You can place them in a subdirectory of the user's home directory with the ~ symbol. See http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html#dpc_variables_url and the rest of this page for details.

Related

Import CSV to MYSQL from a JAVA project folder

I am developing a private web application in Java with a mySQL database. I am having one problem and a question.
As IDE I am using IntelliJ. Now I am at a point where I have to implement a way to insert a lot of data behind each other, but that takes a lot of time. Therefore I thought of prepping a csv file, temporally store it in the project folder and bulk insert it in the DB.
I've found the option LOAD DATA INFILE '/path/to/products.csv' INTO TABLE products;
And for the path I've used
URL url = ClassLoader.class.getResource("sample.csv");
String path = url.getPath();
But the url is always null, even though the class and the csv are in the same folder. What am I doing wrong?
Since I can't get to the next step I wasn't able to test the LOAD DATA way. Is my way possible at all? Or do you have a better suggestion to me.
Thank you for your time!

How to make an installation file from java project and derby database?

I read couple of topics but didn't find a good answer.
I used netBeans IDE and created a program (using GUI)
and using a derby database (threw the netBeans IDE)
and now when I'm finished I want to create one single file that I can send to my friends that contains the app and also the database.
if there an option like this (like creating a runnable jar file + database included).
if not, is there a free-web-hosting to upload my database in?
if there is, please add a tutorial that explains how to do it.
and how to use this databse in my program (what url to get the Connection)
(the database is not big, only 3 tables with some lines in them).
*all of the url-paths I used are project-based (in the project folder - the databse url, files url and so...)
Thank you in advance, sorry for the English mistakes.

Java app connecting to SQLite database

I am developing a very small java application for people on my network. All it does is accept options for user input, grabs data from multiple SQLite databases (no updating to actual database!), and spits out a calculation. It works fine in eclipse but I cannot find a solution for running it as one consolidated application that I can distribute to my coworkers.
I am reading that JDBC is not able to query databases inside jars. I am also reading that it is dangerous and slow to keep the databases on the shared network. Finally, I am told I can create a temporary database when the program runs but I haven't a clue on how get the databases to copy into the temporary ones if the program cannot find them in the first place.
Here is the syntax I am using to access the databases:
Connection con = DriverManager.getConnection ("jdbc:sqlite:src/productLine.db", config.toProperties());
The database is located in the src folder. My questions are:
Is it possible (if advisable) to get the runnable jar to read a database inside of it?
Are there alternative options for this sort of problem?
Am I storing my databases properly or should they have their own source folder?

Execute sql server script from java

I am using sql server, my issue is when I change any stored procedure or database table changes I need to make track of it and need to update it on each and every server when my related java code is uploaded.
So I am finding something tool or something if possible with java code that if I give stored procedure or database changes file(i.e .sql file) it executes it on sql server.
May be my question is silly but I really want to know about is this possible with any tool or through code?
If I understand you correctly, the scenario would be:
Read .sql file using one of the Readers
Execute SQL commands using JDBC and Statement class.

A way to store java files in SQL?

This may be a dumb question, but I have a project for a class where I have to store/retrieve files from a SQL database that connects to a web page. Now, I could just make a webpage to store pictures or music files but I am currently working on creating some basic games in java. I know that there are ways to be able to access these files from a web page, but like I said, the project has to include a SQL database.
So my question is, is there any way to store and retrieve these kinds of files from the SQL database? Being able to download the files would be fine as long as the user would be able to open them, though I would prefer the user be able to open them in the browser.
If anybody has any suggestions I would appreciate it.
When storing into a SQL database, you don't really store the files. You store the file contents. In it's most generic form, you could make a table with a big binary field (a blob or clob depending on which database you use) or a big text field (a varchar) and put the contents of the file into that. Other columns could store file names and such.
To really leverage the SQL database, you would want to know enough about the content of the files to take advantage of indexing and such by breaking it up into more detailed parts. For example, if you are putting a save file in there, you could make a detailed table with columns for username, and all sorts of game-specific state that needs to be saved.

Categories