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.
Related
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
I am working on an android application on android studio, and I am using a sqlite database. The thing is, I want to change the structure of it, so do I just have to change it from DB Browser for SQLite, or I need to change it somewhere else ?
Because the code I am working on isn't mine, and I am just starting to lear android developement
An option is to change the internal structure of the database file from either DB Browser or command line on your computer and then copy it to your phone's storage when the App is running.
If you don't know the steps:
Step 1: Create a new database file from either command line or SQLite DB Browser with your required structure.
Step 2: Place the file in Assest directory in your Project folder. (Assest directory will be in the same folder as the 'res' and 'java' directories are)
Step 3: On Activity Start event, copy this empty database from Asset directory to your database directory which is generally located at /data/data/your.package/databases
Also you can query ALTER TABLE to add columns (as suggested in comments).
This explains the syntax for ALTER TABLE. It also explains other methods for implementing Schema Changes.
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.
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.
Where is Derby database stored when I create it from netbeans?
How Can I combine it with the rest of my project in one folder?
Right-Click on Databases->JavaDB in the Service View and select Properties...
This will open the following screen, where the location is visible and could be changed: