I am working on a simple java app which is using a .sqlite database. The problem is that I don't know where to put the database in the MyProject/ folder and how to make a relative connection path, such that I can use it after I wrap the project into an .exe file. Any suggestions?
This is how I do it now. I have the following path MyProject/resource/EmployeesDatabase.sqlite
Connection conn = DriverManager.getConnection("jdbc:sqlite:resource\\EmployeesDatabase.sqlite");
Related
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 have finally finished my project - a small multiple-choice/vocabulary trainer and I'd like to use it as a standalone jar now.
Problem now is: If I do start it within the build-folder (projectname/dist) everything runs fine.
But when I try to run it from somewhere else I can't connect to the database anymore.
Is there a way to include the DB-files to the .jar file? Or to just copy it to the same folder? Or do I need to have JavaDB installed on the PC's I run it on?
This is the way I do connect to the db:
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/DataBase");
stmt = con.createStatement();
So is there any chance I can create a portable app of this, which I can run eg from an usb-stick?
You do not install JavaDB. However, it will create files, if you tell it to, that hold your DB info. Look at the create=yes option on the JDBC URL. You will have to tell it where to put the files for the DB. They could be on a USB, as long as it is writable.
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 have created a Swing application that uses SQLite as a local database. The database file is located in project's root directory.
Project/DatabaseFile
The application runs fine on Eclipse, but when I run the packaged executable Jar, I get the following error:
No such table : table1
This means that the database is not reachable. When I examined the contents of the resulting JAR file, the database file was not there anymore.
In the code, I've linked the database as follows:
jdbc:sqlite:DatabaseFile
My question is, how to include the SQLite database in the executable Jar?
EDIT
When I placed the DB file in the source Folder Project/src/DatabaseFile and changed the path to jdbc:sqlite:src/DatabaseFile, it worked on Eclipse but again when running the Jar file as java -jar Project.jar. It said:
path to 'src/DatabaseFile': 'C:\Users\name\src' does not exist
I think I need to specify a relative path for the database.
EDIT
This is how I connect to the database:
public Connection getConnection(){
try{
Class.forName("org.sqlite.JDBC").newInstance();
con = DriverManager.getConnection("jdbc:sqlite:src/DatabaseFile");
} catch (Exception e) {
Log.fatal("Méthode: getConnection() | Class : SQLiteConnection | msg system : " + e.getMessage());
}
return con;
}
What library are you using for SQLite?
I did a search based on the connection URI you indicated and found this one. In the documentation it says:
2009 May 19th: sqlite-jdbc-3.6.14.1 released. This version supports "jdbc:sqlite::resource:" syntax to access read-only DB files contained in JAR archives, or external resources specified via URL, local files address etc. (see also the detailes)
If that is the driver you are using, then I would suggest the following connection URI:
"jdbc:sqlite::resource:DatabaseFile"
The key is that since your database is in a jar file, it can not be access as a file with FileInputStream. Instead it must be accessed through the JVM's support for it (namely with Class.getResource() or Class.getResourceAsStream()). Do note that resources contained within jar files are read-only. You won't be able to save any changes to your database.
I have found two different ways to name the filepath depending on how you are trying to access it. Assuming you are accessing the db is located in /yourproject/resource/ or /yourproject/bin/resource ( havent narrowed it down, mine is in both and I'm happy with it) you should use this as your path:
//Eclipse test path
String url = "jdbc:sqlite:resource/mydb.db";
or
//runnable jar path
String url = "jdbc:sqlite::resource:mydb.db";
then
mysqlitedatasource.setUrl(url);
Your way also works... by putting the db in /src
I want to read data from an external .db file in my android app. So I have a .db file and added it to my project folder.
Now I want to access to this file by JDBC, so I added a JDBC.jar file and connected to my DB with
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:myDatabase.db");
and it works fine in a normal java project.
But when I want to do it in an activity in my android project, I always get a ClassNotFoundException.
I checked the class's existence in my android project. It's: 'myProject/Referenced Liberaries/sqlitejdbc-v056.jar/org.sqlite/JDBC.class', so the right path I think.
What's the reason for that exception?