Create Jar file with IntelliJ IDEA that has mysql database - java

I want to create (export) my application into a Jar file to be portable.
How can i put my database contents with jar file?
For e.g for pictures, i put pictures folder beside my jar file, and it shows pictures correctly.
UPDATE
A peace of code to connect to database:
connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "root", "1234");

If you want to distribute a copy of your database with each copy of your application, I think using MySQL will be a bit complicated. You may want to look into using a database system designed to be embedded, such as SQLite, instead. A complete SQLite database is a single text file - you'd simply distribute your one mydatabase.db file along with the jar. See the examples at the above link.

There are two approaches you could use:
Approach 1
Do you really need to use database? If not, store your data on files in file system, that way you can easily export it with data.
Approach 2
Bundle the mysql installation directory in your jar / installer. Write a scripts which starts up both MySQL server and you application.

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

Open paradox / borland database as single file

my question is:
how to connect java tp paradox / borland database ".DB" single files?
Here's what I have:
So, it's Paradox 7 database files.
I'm trying drivers:
http://www.hxtt.com/paradox.html & https://code.google.com/archive/p/paradoxdriver/ as:
String url = "jdbc:paradox:/D:/BABAK/powerGold/SongTitle.DB";
Connection con = DriverManager.getConnection(url);
But both throws exceptions like:
D:/BABAK/powerGold/SongTitle.DB isn't a database directory path!
As you can see, it is trying to find some database folder, but I have only single files! Also, "jdbc:paradox:/D:/BABAK/powerGold" (path to all .DB files folder) didn't work as well.
So, anybody, please help me to figure out, how to open this type of DB in my Java app.
jdbc:paradox:D:/BABAK/powerGold is the correct syntax.
One of the open source Paradox drivers you mentioned is now on Github and has had more features added since a couple of years ago, so that may now work.
If it doesn't, can you post the full stack trace (using this library, not the HXTT one) so we can figure out exactly what's going on? I'm not the original author, but I have made several contributions for different field types.
you're not trying to open the database doing so but a specific file of the whole DB. In fact your DB is composed of files .db, .px ....
The best approach to do so, is to migrate since this DB is not supported, and realy brings a lot of bugs.
I will recommand you to use migrate your database.
install Paradox Database Reader or Editor
export tables to CSV files
import tables in mysql Database (for example)
If you still want to connect this DB without migration with java, share in private a file .db and will give a try now.
To solve it do the following:
String url = "jdbc:paradox:/D:/BABAK/powerGold/";
keep the same files .db and .px of SongTitle in the same directory then run your code and it will work

How to access database from derby in the same directory as jar?

I have developed a movie organizer program in Java with derby database. It works fine on my computer, however I couldn't figure out how to make it "portable" (only for my own use).
My problem is, that if I use the
String url = "jdbc:derby://localhost/Movie";
code for the connection to the derby database, it is only working on my computer as it is the path specified directly to my Netbeans database location. I would like to put the Movie folder near the .jar file of the application and make it portable so I can use the same database everywhere.
Any help would be appreciated on how could I achieve this.
You can access the database from the filesystem for example like this:
jdbc:derby:./myDatabaseName

Using dsnless connection in Java: how to get URL of db inside a jar file and how to put db into jar [duplicate]

I have created a Java desktop application which reads and should write to a Microsoft Access DB.
The application works fine before I convert it to a .JAR after which it can only read from the DB but doesn't write to it.
Any ideas on how to solve this issue?
I am guessing you've included the database file in the JAR file itself. Simply put, although you can get a URL to read a file from inside a JAR, you can't write to one. You're going to need to take the database (MDB file?) out of the JAR and put it in on the actual filesystem if you want to write to it.

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.

Categories

Resources