Is it possible to connect to database from library jar - java

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

Related

how to create jar file with embedded database H2?

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.

Create Jar file with IntelliJ IDEA that has mysql database

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.

how to include mysql db inside eclipse project

I have a dynamic web java project, which needs to talk to a database
i want to include the created database file inside the project, so that i can access the data inside and the db isnt on some server
is it possible to do that? if so, how ?
For eclipse, open view Data Source Explorer and add the 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.

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