Java DB path for GitHub distribution - java

I have a project assignment in which I have to create a Java application. The program must be uploaded to my GitHub repository so my professor can see it and interact with it.
I have the following problem: For the purposes of the application I have to use a database. I decided to work with a Java Derby embedded database. So here an example of my code where I connect with the database:
String Table_click = (jTable1.getModel().getValueAt(row, 0).toString());
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
java.sql.Connection con=DriverManager.getConnection("jdbc:derby:C:\\Users\\themi\\OneDrive\\Desktop\\HRCompanion\\databases\\Login", "root", "1234");
As you can see, my database is stored inside the project folder on my computer. But if I upload this code to GitHub, then my professor will not be able to run the application properly, as the database path does not exist. Is there a way to change the path to make the application work properly to a different computer?

You can use a relative path in your JDBC Connection URL.
So, for example, jdbc:derby:db means that Derby will look for the database in the db folder in the current directory.
Then you won't have such a problem with system-specific pathnames.

Related

apache tomcat creating new instance of sqlite database in java

I am using an sqlite database in java using xerial sqlite jdbc driver in order to store some data, 1 connection at a time.
It worked fine until I decided to use Apache tomcat on my server instead of java sockets (used for testing) and I noticed my queries yielded null results despite data being on the database.
I searched this issue and apparently tomcat creates a new instance of this database, which has the same tables but has no data.
I tried moving my database into the resource folder but this did not help.
public static Connection getConn() throws SQLException {
if(c == null){
c = DriverManager.getConnection("jdbc:sqlite:database.db");
}
return c;
}
this is how the code connects to the database. I believe moving the database and changing the address should solve the issue but I dont know where to.
thanks!
You should be able to use the full path to your existing database file in your JDBC URL - so, something like:
"jdbc:sqlite:/path/to/your/mysqlite.db"
If you are on Windows, then it might look like this:
"jdbc:sqlite:C:/path/to/your/mysqlite.db"
These are examples of absolute paths.
In your case, the database location in your JDBC URL is simply mysqlite.db, with no additional path information. This is a relative path, because it does not start with a slash or a drive letter.
This means the location of the DB file is relative to the working directory of the Java program (where the program was started). In the case of Tomcat, that is typically the CATALINA_HOME/bin directory - where CATALINA_HOME is typically the location where Tomcat was installed. (This may vary depending on how Tomcat was installed and launched.)
I would recommend you keep the database file in a separate location from Tomcat.
(SQLite will create a new, empty database in the specified location, if one does not already exist - so it's not Tomcat which is creating it, but rather the SQLite driver).

Get JDBC connection working in play framework (java)

Was wondering, what steps am I missing to get a jdbc embeded h2 database working in my play application? Following these docs.
So far editted Application.conf file to contain this:
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:databases/test"
db.default.user=test
db.default.password="testtest"
Next I created a libs directory and added the jar file
h2-1.3.174.jar
Is this necessary or does the provided driver handle all types of h2 databases (embeded and server - I know it handles in memory)?
Now in the controler how can I access the database? Do I have to start/shutdown the database?
I know I can get connections from the getConnection() method in play.db. But everytime I execute a statement through this connection I get an exception saying no data is available. If I then check - looks like directory
databases/test
was not created so no database files exist.
What am I missing?
H2 works out of the box. Just create a new project in the terminal.
Otherwise, to your listing:
I think you should change db.default.url="jdbc:h2:databases/test" to db.default.url="jdbc:h2:mem:play"
don't need to create lib directories. It's all handeled by the build in dependency mgmt sbt
Just use the model objects and call save/update. No need to call start/shutdown
you are in a framework, it's all there ready for you...
I think you should start reading the documentation from the beginning to the end and examine the example applications. It's all there what you are looking for.
In addition to myborobudur's answer I'll only mention, that you don't need to use memory database, as you can for an instance use file storage (Embedded) or even run H2 as a server and then connect to it with TCP in Server Mode... Everything is clearly described in H2 documentation.

How to access an existing SQLite Database from another project in Eclipse

I have 2 projects in Eclipse.
The first project calls a method from the second project handing it an object the second project shall write into an existing SQLite Database residing in the second project.
However, I get the following error:
opening db: 'tomato.db': Zugriff verweigert
Zugriff verweigert is German for access denied.
How can I allow the db access from the first project to the database file tomato.db residing in the second project?
Solution
I use sqlite-jdbc from xerial.
In their tutorial they get the database connection with this line:
connection = DriverManager.getConnection("jdbc:sqlite:yourdatabasefile.db");
However, this doesn't work from another project in Eclipse.
The solution is actually pretty trivial:
connection = DriverManager.getConnection("jdbc:sqlite:C:\\path\\to\\your\\database\\file\\yourdatabasefile.db");
Another solution is to use the in-memory sqlite database like this:
connection = DriverManager.getConnection("jdbc:sqlite::memory:");
Hope this helps.

how to embed javadb or hsqldb into java application with hibernate using netbeans?

I successfuly embedded javadb in my application using the classpath ,but here's the problem : I want hibernate to be able to work with the database,but I always get an error in netbeans sayng "enable to establish connection ".
AnyHelp please ?
The URL for a local HSQLDB database is jdbc:hsqldb:file:file_path_name The file_path_name is usually an absolute path with a name at the end. The database engine will then create a few files with the given name, but predefined extensions. An example of this is: jdbc:hsqldb:file:/mydata/mydb which will produce mydb.properties , mydb.script and a couple other files in the /mydata directory.

Best way to access a sqlite database file in a web service

First question from me on stack overflow.
I have created a java web application containing a web service using netbeans (I hope a web application were the correct choice). I use the web application as is with no extra frameworks. This web service use a sqlite JDBC driver for accessing a sqlite database file.
My problem is that the file path end up incorrect when I try to form the JDBC connection string. Also, the working directory is different when deploying and when running JUnit tests. I read somewhere about including the file as a resource, but examples of this were nowhere to be seen.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
I don't know much about web services, I just need it to work, so please, help me with the technicalities.
Update
To put this a litle bit in context, some "println" code gives this:
Printing the work directory from a simple JUnit test gives
C:\MinaFiler\Work\SOA\BusTimetableWS
Invoking a similar web servic method returns
C:\Program Files\sges-v3\glassfish\domains\domain1
The connection string is formed from prepending "jdbc:sqlite:" to the path which at the moment is absolute:
C:\MinaFiler\Work\SOA\BusTimetableWS\src\java\miun\bustimetable\database\sqlit\BusTimetableWS.db
However, this fails because my tests throws exceptions stating database tables doesn't exist although they really do, I can see them with sqlite3.exe .
One way would be to use a config file that you can read and fetch your connection string from there.
I'm sure the framework you are using has some kind of standard way of saving configurations.
Another option would be to place the db in a known relative path from your main execution files. Then when executed fetch your current directory, and look for the db from that path.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
The web service should use a DataSource to retrieve a connection from a connection pool configured at the application server level. In your unit test, use whatever you want (a standalone connection pool, a direct JDBC connection).
But in both cases, why don't you use an absolute path to the database file in your jdbc url? From How to Specify Database Files:
jdbc:sqlite:C:/work/mydatabase.db
The working directory wouldn't matter if you do so.

Categories

Resources