I want to create some applications in Java netbeans using sqLite manager. i had done the following steps.
I have done plugin to Mozilla Firefox a sqLite-manger database
I have create database calling "mydb".
I have create a table with 2 values fname , lname.
I got mydb.sqlite file.
In netbeans library i have add jar file calling sqlite-jdbc 3.7.2 jar
then i copy the file mydb.sqlite from the folder and paste into netbeans project folder .
I want to connect with my project calling "test" in netbeans.
how to connect with netbeans application
Here is example http://www.tutorialspoint.com/sqlite/sqlite_java.htm
Most important part is jdbc url to create connection:
jdbc:sqlite:mydb.sqlite
This url assumes that test.db located in same directory, u application starts from. But u can put path to certain db.
For example:
jdbc:sqlite:c:/temp/sqlite/mydb.sqlite
If u distribute u application when there u manage some initialization. Actually u have to set environment for u application before start. Let's say u have good working application in netbeans and u copy application to another machine.
Configure environment for application to make it same as actual working application instance in netbeans. Question u should think about
What path to database to use relative or absolute?
Where database location on filesystem (absolute path to database)?
Where directory from which application starts (relative path to database)?
Useful information:
jdbc driver will create database mysql.sqlite if it doesn't exists (path to directory "c:/temp/sqlite" should be real) and after u can recreate necessary structure of database.
u can use System Properties https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html especially user.dir and user.home to set predefined location of u database (create database in there or copy existing database file)
Related
I am working on a JavaFX application in which I am using SQLite database for storing some important information. Since I am working on IntelliJ, the created database file is in the root directory of the project.
SQLite database location :
jdbc:sqlite:database.db
But where will be the database after installing with .deb or .exe files by default? In Linux and Windows.
How to set a location for a database. So that, that location is going to be the location of the database after installing the application with .deb or .exe files.
you have three options.
the first is to pave the path to the database as an app parameter.
the second option is to use the user's home directory. you can create a subdirectory whose name starts with a dot (hidden directory in linux) in which to put your work files.
to get the name of the home directory use
System.getProperty("user.home") ;
the last option is to use the directory where the jar file is located
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Maybe it's a silly question, but I am new to databases and I don't know how to do this.
I have the following line of code that I use in order to connect to a database located in my D: drive. How can I change it so it connects to the database if the database is located inside the project folder?
(I use netbeans IDE)
Connection conn = DriverManager.getConnection("jdbc:sqlite:d:\\Databases\\DataBase1.db");
//I don't want an exact path, I need it so it works idependetly of where the project folder is located
According to this answer:
In your code you can use a relative path and it should be relative to the top of your project's directory. In NetBeans' Databases Service you will need to either use an absolute path to connect to the database that exists (in your project) or you will need to provide a relative path...
So according the SQLite Tutorial connection string should be as follows:
jdbc:sqlite:DataBase1.db
I'm trying to setup ObjectDB. I am able to create a database and view it with the explorer, using the embedded-server mode to be able to keep the explorer open while running my program. This all works fine, when my database is in the $objectdb/db/ directory.
However, I'd like to be able to do this when my database is in another directory (thus not in $objectdb/db/). When I'm not using the explorer, the database looks fine. I can also open the explorer to view the database. But... I can not keep the connection to my database open in the explorer while running my program (and thus making changes to the database).
What I have tried/have been thinking about:
The database and the explorer have to use the same .conf file. I think the explorer uses the conf file in the $objectdb home directory, but I can't figure out how to configure my database in the other directory to listen to that conf file, too. How can I create a project (or database) specific conf file for a database that's not in the $objectdb home directory?
In general it feels strange to me that there would be one conf file for all your ObjectDB databases.
I copied both the explorer.exe and objectdb.conf to the directory my database is in, hoping this would work. However, nothing happens when I try to run the exe file. I think this is because the exe (and the explorer.jar, tried that one, too) rely on objectdb.jar, but I couldn't find that anywhere. I found objectdb-2.7.1_01.jar and copied that into the directory the database and explorer were in, but that didn't help.
As for an MWE, I followed this tutorial, using IntelliJ.
Try the following:
Open the database in embedded mode with a path that specifies a free port for the embedded server, e.g. "$objectdb/db/my.odb;port=9999"
Access this database in client mode using "objectdb://localhost:9999", or in the Explorer, select File -> Open C/S Connection, specify 9999 as the port and keep the database path empty or /
objectdb.jar is available when you download ObjectDB as a zip file (in which you also find the Explorer), you can also rename the file from Maven, which contains a version number to objectdb.jar.
I created a software using NetBeans and SQLite database. When I clean and build, the executable jar file and database work fine. It can read and write data from the database.
Then I created .exe file using "install creator" and after installing the software, the same dist folder is created in Program files on my Windows PC. When I run the executable jar file from that dist folder, it can only read the database, but can't write. I get this message
java.sql.SQLException:attempt to write a readonly database
Can anyone please help me solve this problem? Thanks in advance.
check this
The problem, as it turns out, is that the PDO SQLite driver requires that if you are going to do a write operation (INSERT,UPDATE,DELETE,DROP, etc), then the folder the database resides in must have write permissions, as well as the actual database file.
I found this information in a comment at the very bottom of the PDO SQLite driver manual page.
You should write the user specific data to the Application Data folder of current user.
You can get the ROAMING directory from
String path = System.getenv("APPDATA");
Or if you want to make it platform independent you can use getProperty which will give you users home directory and then you can write to specific directory:
String path = System.getProperty("user.home");
You can form the sqlite on path like
String sqliteUrl = "jdbc:sqlite:"+path+"sample.db";
Use this code line:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\databasefile.db"
I can say this is the proper way of creating DB in application folder on to the drive like C:\ without permission
Use this code line:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Yourfile.db"
I can say this is the proper way of crating DB without permission on to the drive like C:\
I'm using H2 embedded database for my application. I would like to contain everything the application needs in it's own Jar, including it's database if possible. My app does not need to create temp files or anything, so basically the user just runs the Jar.
Is it possible to embed a database inside a Jar, and be able to INSERT new records as well as just SELECT out?
EDIT: Just to clarify, I'm not looking to embed the H2 driver jar inside my distributable jar, I'm looking to embed the h2 database file (someDatabase.h2.db file) inside a Jar and still be able to write/read from that database.
If you wish to embed the myDatabase.h2.db file inside the .jar, you can do so, but you'll have read-only access to the database. As .jar files are read-only, you can't modify them and therefore can't execute INSERT, DELETE or any DDL command.
That being said, below is an explanation on how to embed it read-only.
According to H2's documentation:
The JDBC URL "jdbc:h2:~/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the home directory of the current user.
The JDBC URL "jdbc:h2:file:/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the current directory (where the java program was executed).
If you embed the h2.db file inside a .jar, it is not accessible in a plain way. It is only accessible as a file inside a zip file.
In order to make H2 uset it, you have to use a zip as URL:
jdbc:h2:zip:~/data.zip!/test
See more in "Read Only Databases in Zip or Jar File".
When you embed the file as a resource in the jar, you may get it's relative url. Using...
MyClass.class.getClassLoader().getResource("myDatabase.h2.db")
...you'll get something like:
jar:file:/C:/folder1/folder2/myJar.jar!/myDatabase.h2.db
You can then manipulate it as a String and pass as JDBC URL connection to H2.