how to create jar file with embedded database H2? - java

I have created javaFX project with embedded database H2. And I have created jar. On my computer this programm works. And on other computer this programm runs but buttons doesn't work. I think they dont work because project doesn't see database. I cant understand how to build project right with dependencies(database). Plz help me to build jar with database!!
P.S. I dont know how to use maven.

This might not be the solution to the root cause of your problem, but to include a database in a jar file can be done in the following way:
Theory: Execute a script that creates and populates the database when the application starts. This should be done only once, on the first execution. Further executions should use the existing database and data.
How To: use flyway or another tool to executes the script automatically calling a simple method.

Related

Is it possible to connect to database from library jar

I have a problem statement. I am trying to create a library jar which will help anyone to generate report from data. I am creating a springboot java jar for same.
Inputs : SQL statement and Type of report (CSV,PDF etc)
Now the problem is I want this utility to be used by multiple projects/Applications. But the problem is I am not aware how I can connect to database of the project or application which is using this library. I can not provide all the datasources b'cos that is not possible for me. There can be 100s of application which can be using this utility jar so no way i can create that many database connection in my jar and also what if some of the datasource changes in future.Is there any generic way which I can use to achieve same or is this possible somehow to build jar in a way which will pick and connect the datasource of the application or project which is using it.
P.S. are there any tools which i can use to help me out with the same problem statement i.e. pass the sql and it will connect to database for me and get me the results.
Yes you can achieve this by putting all your configurations in a file in every project. Your jar file will use that file and connect to the relative database according to that project configurations.
Your Configuration file i.e; named db_connect.properties should be same in every project you are using. Inside this file there should be keys and values, which will be used by the jar to connect to that particular database.
Here i am showing my database.properties file for a project which is used by a jar. My custom jar is using some extra features, you can ignore them.
# Database configuration
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test?autoReconnect=true&autoReconnectForPools=true
username=root123
password=root123
partitionCount=2
maxConnectionsPerPartition=4
acquireIncrement=1
acquireRetryDelayInMs=10000
connectionTestStatement=SELECT 1
connectionTimeoutInMs=90000
idleConnectionTestPeriodInMinutes=30

Javafx SceneBuilder not shareable or derby not connecting in .exe

So I am a new programmer in my second term of Java programming. The term is over, but some of us students turned in our final projects only to find out when we gave over our projects some of the .jar files do not seem to follow the project.
We did all of our work in Netbeans because these were the rules and when using SceneBuilder things went wonky.
The solutions I'm looking for are:
How can I share this project with my teacher and have it work on a different machine than mine, (I did make this into a .exe file and .jar file, not a complete solution).
If number 1 can't be done, how do I get my derby database to work in the .exe file?
When the .exe file executes the GUI pops up and I have full functionality as I would when running in Netbeans. Cool right? Ehh. Only problem is the derby database does not get built as it should. Then the select statements do not get called, because there is no DB to connect to.
Any help would be amazing and if a solution for sharing the non .exe in Netbeans can work I think it will help my instructor also.
If your are using derby in an Make sure you have derby.jar in your class path. Am assuming you are using derby in an embedded environment if not please clarify. Make sure you connection path points to the location of your database

Creating an installation for a java project with H2 embedded databae

I created a java project that uses a database to retrieve, edit, and save data. I finished the project using Netbeans and now I want to create an installation out of the project. To do so, I want to include the database to be installed along with the project. The code I am using to connect to the database is:
Class.forName("org.h2.Driver");
Connection con = DriverManager.getConnection("jdbc:h2:~/localhost/gallerie", "root", "123");
The application is working fine on my pc, but I need it to be installed as an executable on other pcs.
What is the best way to do so?
One way to do this is with the setup packager from stardust software. You will also need to include some script or some Java code to create the database and the database user. You don't need to do that with the installer. Instead, create some file or store something using the Preferences API so the program knows when it is running for the first time. When your program runs for the first time it needs to call some function to create the database user and the database.
Alternatively, you could also package the database files with your application as described by this stack overflow question.

Java app with embedded DERBY DB

I am trying for a while to make executable JAVA application having embedded DB (derby DB), but facing some problems, and need your valuable help.
Namely, I am using Eclipse as environment.
I export Java app to RUNNABLE JAR file, and it works fine on my desktop machine.
The idea is to make EXE doubleclick icon and send it to another machine which have no JAVA background/environment....so point is to send it to another user who will just get exe file, double click it and work with it.
The DB is not only readable, since application is inserting data in tables.
So, it works fine on my machine, but when I send the same JAR file to another machine, I get error:
"Schema TEST does not exist"
I can see application but without any data, like there is no connection with DB. So, it is useless.
Even I use JSmooth, Install4j.... to convert JAR to exe file, I get the same error.
So, first I have to make JAR file working on another machine.
Seems to me, I am doing something wrong with DB files.
Please let me know what info u need more from my side, and let me know how I can do this.
If the application intends to read AND WRITE data when it is running, then it can't be entirely contained in a JAR file, because you can't put an updatable Derby database in a JAR file. You'll need to have some "installation procedure", perhaps some logic that you run the first time your application is double-clicked by the user, that (a) finds a suitable location for the Derby database on the user's machine, (b) creates the database and defines all the tables, views, etc, (c) loads any initial data.
Then, on subsequent runs of the application, it will be able to re-open the database and continue using it.

how to add mysql db to my project?

I write a program with netbeans.
I connect my program to mysql an use from db that I made in mysql and it work correct.
Now I create .jar file from my project and it works but didn't connect to mysql and don't show any data but I am sure that I have information in my database.
What is problem?
You need to do more debugging. Make sure you are catching and logging or printing out exceptions. They should indicate a more specific problem. Make sure you have all the needed drivers and depended libraries in the class path of the .jar you are trying to run.
May be you missed to pack MySql jar in your jar.
What error do you get? Its hard to say proper solution with the information you have provided.

Categories

Resources