how to make executable java app with sqlite - java

i am working a java app using an sqlite db, with eclipse.
i produce an executable jar with library handling: copy required libraries into a sub-folder next to generated JAR
the problem is that the app runs ok in my computer, but when i try to run it in another computer doesnt connect with the db.
(in code i put url for db E:\\ and i put also the .sqlite file in that location in my and the other computer)
String url = "E:\\";
this.dburl = "jdbc:sqlite:"+url+"\\converted.sqlite";
i want the .sqlite file to be out or jar file, so i can back it up. Any ideas?

I always like to use unix style forward slashes in path and it also work in windows. you are putting an extra slash in your db url. Try to put these file into C drive because other PC might not have an E drive. Try this:
String url = "E:/";
this.dburl = "jdbc:sqlite:"+url+"converted.sqlite";

Related

How to connect to a database located inside project folder

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

Use database in embedded-server mode outside the `$objectdb` home directory

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.

How can I make my java code machine independent

I have written a very small java code on Eclipse which will automate a small process of logging into a web system. The employees of my company use this web system to connect to office network if they are working from home.
I have converted my java project on Eclipse into an exe file, my intention is to log into that system by just a double click on the exe file.I have parameterized the userID and password and have stored it in an excel file on my local machine.
The problem am having is, My exe file will not run in any other systems except mine as my code is referring to the excel file(which has userID and password) path on my local machine. I would greatly appreciate the developers on this forum who could help me out to come up with a solution for this problem.
What about looking for the excel file in a well defined folder like C:\Users\\my-tool\credentials.xls. Or maybe look for it in the same dir as the executable?
You can get the path of the home folder of the current user with this command:
String homeDir = System.getProperty("user.home");
With that you cann assemble your custom lookup path:
Path xlsPath = Paths.get(System.getProperty("user.home"))
.resolve("my-tool")
.resolve("credentials.xls");

How to change the read/write permission of SQLite database on windows?

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:\

Can't find folder or file created with Android App with windows file exlorer

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer.
I CAN find and read the file using my phones file manager but not using the file browser in windows.
So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.
I have a uses permission for the application to allow it to write to external storage.
This is the code I use to create the file and directory.
String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";
File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
}
Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.
I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.
If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.
More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.
I blogged about this last summer.
Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem.
I have met the same problem, and I use the method in the comment
And it works for me.

Categories