Include JavaDB into .jar file (Netbeans) - java

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.

Related

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.

Error on H2 Database when I converted my Jar file to Exe

Here's what I did:
I build my project and its dependence Libraries into one Jar file
I Convert it to exe Using VJtoExe and Launch4J
The Problem is, When I run the Exe file, the system connects to my H2 database but it can't find the tables in it.
This is how I connect to my Database
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:~/test;AUTO_SERVER=TRUE;IFEXISTS=TRUE;", "admin", "denrlb");
stat = conn.createStatement();
Update:
Now I use Server Mode, in NetBeans it works fine, the server start automatically, but when i compiled it and convert the .jar to .exe the server does not start automatically, to make it work, i need to open the h2 manually,
how can i make it open the server automatically?

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

Swing Java-sqlLite relative path connection

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");

Java Applet on an Xampp Server - No suitable driver found on jdbc:mysql://localhost:3306/

I'd like to preface this question by saying that I'm really new at server setup and programming.
I made an applet that views data from tables in a MySQL database and lets just say it outputs it. It's a lot more complicated than that but the point is that the applet runs perfectly in Eclipse when I run it. I had to set the MySQL-connector-java-5.1.15-bin.jar file as an external library for it, but after doing that it runs perfectly.
The problem is whenever I put the code on my Xampp server, it doesn't work right. The applet start and everything works until the applet tries to connect to the MySQL database (Xampp) using the following code:
String connectionURL = "jdbc:mysql://127.0.0.1/";
Connection connection = null;
try{
connection = DriverManager.getConnection(connectionURL, "root", "mypassword");
}catch(Exception e){
System.out.println("Error: "+e);
}
In the applet I didn't use System.out.println(), but I did manage to find out the I was getting was:
java.SQL.Exception: No suitable driver found on jdbc:mysql://127.0.0.1/
I'm wondering what I need to do to get around this error. Where do I need to put the .jar connector or how do I need to configure it with my Xampp server to get this to work?
To build a webapp, you would compile your code and build a WAR (Web Application Archive), which the artifact that would be deployed to your server, unpacked, and be put in your webapps folder. WEB-INF is the source folder where your web content goes in your project.
In eclipse, you should have this folder src/main/WEB-INF, where you could place CSS (Cascading Style Sheets), JavaScript files, images, JSPs, etc.
The fact that you have added the MySQL jar as an external dependency means that it would not be added to the WAR that you would be deploying.
The short answer to your question is that you need that MySQL jar to be on the classpath of your webapp, which you would do by placing it in (e.g.) tomcat/webapps/<your project name>/WEB-INF/lib, then restart tomcat (that is, if you use tomcat).
This will be a bit of a learning curve at first, but I would suggest you learn how to use Maven. Maven will allow you to consume dependencies, and build a proper WAR (pulling in these dependencies).
Guide to building a webapp with Maven.
I fixed it!
All I had to do was this:
<applet code="file.class" archive ="mysql-connector-java-5.1.14-bin.jar" width="500" height="500">
</applet>
in the html file that I wanted to display the applet in.

Categories