Java app connecting to SQLite database - java

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?

Related

Where should I place my sqlite database in my java maven project

Since putting in resources folder made the database in to read-only. I wanted my database to be in the jar file.
As noted in comments by James_D:
The contents of the resources directory will become part of the jar file. And anything placed in the jar file is necessarily read-only.
How to rectify this depends on what you want to do.
You can install it on another machine and access over the network.
You could create a new database on the local machine.
see System.getProperties() documentation for finding local file locations.
If you want to seed data from an existing database in resources, then copy it out.
If read-only mode is sufficient, you may be able to access the db in read only mode when it is stored in a jar, though I wouldn’t guarantee that it would work as expected.
Beyond these generalities I don’t think there is specific info to be provided without more specifics on your app.
For a tutorial on connecting JavaFX and SQLite:
eden coding JavaFX db tutorial.

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.

Is it possible to access an SQLite database or (Sql browser for SqLite) from Java?

I want to work very portable, so I don't install a monster of Sql Server which has 2.7Gb size on my computer. I want to find an easier solution. I have for example a form application in Java which must to access a database (maybe a server), I thought it's easier for me to work with a portable database (Access or SqlLite).
It can be an installer solution but not very big such as 1Gb size or more. Initially thought to use a text file or Excel for keep the data but this way is hard too because I can't easily simulate the constraints and relationships between lines and tables.
Of course it is possible to access an SQLite database from Java! As you can read here:
A database in SQLite is a single disk file¹.
So you just need to use the driver to read/write from/to this database (file).
The code in Java would look like the following (taken from here):
How to Specify Database Files
Here is an example to select a file C:\work\mydatabase.db (in Windows)
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");
A UNIX (Linux, Mac OS X, etc) file /home/leo/work/mydatabase.db
Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");

How do I store data in Java without need of extra software?

I am currently developing a program in Java using NetBeans that manages expenses. In this program I used MySQL to store data. Now I want to ask that if I give the program to one of my friend, he would also have to install MySQL using the same password that I used. Is there a way in which he will not be required to install MySQL?
Now suppose if my friend already has MySQL, but with a different password. My program would not work in that case, and it would be hard to replace my password with his password in the code. Is there a way to make the program run on his PC?
Earlier once, I have used an Oxford dictionary program. That time I did not have Microsoft Access installed. When I installed Microsoft Access I came to know that all the words of the dictionary are stored in a Microsoft Access file. How can I do this? I.e. how can I use MySQL/Microsoft Access to store data without the need to install either of them?
You can use an in-memory database like H2 Database if you don't require a large amount of data
But I think you should make your db connection configurable by using a properties file
If you want everyone to be able to use the database, you need to run it on a server that people can access through the internet.
However if you don't care about them using the same database and just want them to use their own, you could for example create a small file named "config.ini" or something like that and put the login information (like the password) in there.
Then just read it in your code and use the info you read to log into your database.
That way, every new user will only have to change the config.ini file without ever having to touch the code.
The best solution in my opinion would be SQLite as it is light, and data can be stored locally in a desired location in a single file. H2 is more likely to be a developer tool.
This solution does not require additional software to be present on the user machine. Of course it has its limits, but as a simply storage for program dynamic data it is a good solution. It is worth mentioning that Android applications also can store their data in LiteSQL. It is a bit different approach there, but the principle stays the same.

Using HSQLDB as a portable database with Spring, Hibernate

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.

Categories