I am setting up an SQLite connection in Java in Netbeans. When I am writing the absolute path in the connection.java file, it works just fine. How do I write this as a relative path so that I can share the project with others as well?
Connection conn = DriverManager.getConnection("jdbc:sqlite:/Users/sparshbohra/Desktop/214/214-project/src/baby/pluto/RoomBookingDatabase_V1.1.db");
This is what the folder looks like:
I am getting an error when I use the following link as a path:
"jdbc:sqlite:/RoomBookingDatabase_V1.1.db"
Related
I'm using NetBeans and this is my project folder:
My executable is in the 'dist' folder. About the code,in my connection class I used the following string:
"jdbc:ucanaccess://.\\mydb.accdb"
So i used a relative path from dist folder.
This works the first time i execute the program and correctly connect to database, but the next times i have the following exception:
"given file does not exist: .\mydb.accdb"
Obviously the file exists and it works the first time as i wrote. I'm also sure i'm doing something wrong. Can someone help me? Thank you
I've solved. I was using a relative path starting from the dist folder while I had to use a relative path from the folder where is the connection class.
I am trying to use an H2 database with Java. I cannot seem to find out where the data is being written.
The database URL I am giving is: jdbc:h2:/db/bh.
I am connecting with the database using Java like so:
dbObj.setDBConnection(DriverManager.getConnection(hObj.getDBUrl(), hObj.getDBUsername(), hObj.getDBPassword()));
where DB URL is given above.
Username: sa
Password: (empty).
I am running the jar from within the following folder:
C:\work\sampleH2\sampleH2.jar
My understanding of the FAQ section of H2 says that the database bh will be found in the folder db/ of the folder sampleH2. But that is not the case. Where can I find it?
according to http://www.h2database.com/html/cheatSheet.html there is a difference between storing in:
relative path (somewhere under current directory): jdbc:h2:test
absolute path (somewhere under root directory): jdbc:h2:/data/test
so i would look for it on your main drive (probably c:) under path you specified
Lately I have been looking into having a servlet with a local database. With a bit of research I found H2 Database Engine (Wikipedia). This is perfect for what I want but I am having trouble with the local path for my servlet.
Example:
I need to create the H2 Database in my WebContent folder so its apart of the project. However I cannot seem to get the code right to localise it.
Example CODE: - H2.Jar - Connection String to SQL Database
String url = "jdbc:h2:"+request.getContextPath()+"/emailDB;IFEXISTS=TRUE";
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection(url, "adminuser", "pass");
Example CODE (ERROR): - H2.Jar - Connection String to SQL Database (OUTPUT)
org.h2.jdbc.JdbcSQLException: Database "C:/emailservlet/emailDB" not found [90013-174]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)
at org.h2.message.DbException.get(DbException.java:172)
at org.h2.message.DbException.get(DbException.java:149)
at org.h2.engine.Engine.openSession(Engine.java:54)
at org.h2.engine.Engine.openSession(Engine.java:160)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:139)
at org.h2.engine.Engine.createSession(Engine.java:122)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:323)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:105)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:90)
at org.h2.Driver.connect(Driver.java:73)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at emailservlet.msdbcon(emailservlet.java:540)
As you can see the issue i am getting is that even though im requesting the contextpath i am still getting C:/ written before.
If you can help me figure out the error in my code that would be so helpful!
Thank you in advance!
The driver expects file system path where it can create files. It's converting the relative path to absolute by using root directory that is your C: drive. To get absolute path to WebContent folder you need to use ServletContext#getRealPath()
Also it's not a good idea to store H2 file's in WebContent folder you should store them in WEB-INF folder so that its not accessible to users.
Below is how the url should be formed
String path = getServletContext().getRealPath("/") + "/WEB-INF";
String url = "jdbc:h2:"+path+"/emailDB;IFEXISTS=TRUE";
This will create H2 files in WEB-INF folder.
Considerations taken from the Features page on H2Database.com site:
The database URL for connecting to a local database is jdbc:h2:[file:][] . 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).
The database name must not contain a semicolon.
To point to the user home directory, use ~/, as in: jdbc:h2:~/test.
I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file.
I've try to get connection to the DB by this line:
DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())
The DataController is a class that located where the sqlite located.
and i keep get an error:
java.sql.SQLException: invalid database address
Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file?
Apparently sqlite-jdbc can open resources on it's own. Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk, add :resource to the path. So try the following:
DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite");
or depends on version of sqlite
DriverManager.getConnection("jdbc:sqlite::resource:package/test.db");
Replacing package with the '/' separated path to the package this file is in.
Be aware that it will actually copy the file to a tmp directory.-
The ::resource way is right. And these explanations will help if you are using ::resource but still get errors like resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana.
Most common anwsers are:
DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db")
However the path/to/database.db must be the exact path(the Path in Real File System but not in Jar) to your jar file.
I recommend to use getClass().getResource():
DriverManager.getConnection("jdbc:sqlite::resource:" +
getClass().getResource("/path/to/db/in/the/jar/file"))
NOTE:the /path/to/db/in/the/jar/file part must start with /
I'm not sure if this has changed in recent JDBC versions, but after fiddling with it for about an hour and getting various exceptions (MalformedURLException and "Database has been closed"), I found that the following worked for me:
DriverManager.getConnection("jdbc:sqlite::resource:file:/path/to/database.db")
The :file: portion seems to be missing from other answers and I couldn't get it to work without it.
Also, note that the
/path/to/database.db
is the absolute path starting from the root of the .jar file, rather than a normal resource root.
jdbc:sqlite::resource:notes_app.db
worked for me. My database(notes_app.db) was in the root of generated jar file.
public class Database {
public static Connection con () throws SQLException {
String url = "jdbc:sqlite::resource:notes_app.db";
return DriverManager.getConnection(url);
}
}
My notes_app.db is in the root of generated jar. I'm developing with maven and notepadd++
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