How to store the data within java application? - java

I am using H2 database which is embedded in my java application. I'm creating the connection to the server as:
jdbc:h2:file:/mydata
Where mydata is the database name. This seemed to tell the database connection caller to find the database within the same directory as of the application running from. But it cant find it on client computers. Why? What to do? Where to save the database so as to I don't lose data when I distribute my application?

According to documentation you do not need / before mydata you you need to look up for a file in the same directory
The database URL for connecting to a local database is jdbc:h2:[file:][path]. The prefix file: is optional. If no or only a relative path is used, then the current working directory is used as a starting point. The case sensitivity of the path and database name depend on the operating system, however it is recommended to use lowercase letters only. The database name must be at least three characters long (a limitation of File.createTempFile).
http://www.h2database.com/html/features.html#embedded_databases
So in your example you are trying to connect to file named mydata in the root folder. Looks like you forgot a dot (.) before /mydata. Try with the following jdbc:h2:file:./mydata

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).

Java DB path for GitHub distribution

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.

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.

Access to WEB-INF in a Java REST WebService

I'm creating a Java REST WebService which obtains data from a local SQLite database allocated in WEB-INF/data directory (This design is a requirement).
The problem is that I can not reach the WEB-INF directory.
I think I need to get the absolute path but I don't know how. I'm not able to get the Application context to get the app root path.
I'm using jdbc to connect the database:
conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
So I need to assign a value to dbPath variable.
How to get the absolute path in a Java Rest WebService??
If I get your answer well, try to use:
getServletContext().getRealPath("/WEB-INF/data/filename");
Or
getServletContext().getResourceAsStream(...);

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