Reputation: 567
I'm trying to build a simple java application for my school project, and I want to be able to use a simple DB, in order to insert, update, delete and ask queries on my DB.
I need my application to run everywhere on installation, so I want to use a local DB that ships with my application, and will be accessible from inside the project, without different DB dependencies
so I've read a little and found this SQLite tutorial -http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/
, now I want to set a relative path to the user who download my application, and set the connection on the user computer. I've noticed it's written :
connect to an in-memory database, you use the following connection string:
jdbc:sqLite::memory
here is my code:
public class Main {
public static void main(String[] args) {
try{
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Desktop\\School Project\\Software-Engineering---Java-Project\\data.db");
Statement statement = conn.createStatement();
//CREATE TABLES
statement.execute("CREATE TABLE IF NOT EXISTS CUSTOMERS (name TEXT,phone INTEGER,email TEXT)");
//INSERT TO TABLES
statement.execute("INSERT INTO CUSTOMERS (name,phone,email)" +
"VALUES ('NETANEL', 05555555,'SADF@GMAIL')");
notice how my JDBC is the path to my local computer, how can I change this?
EDIT:
I'm able to set the path with only referring the DB name:
final static String DB_URI = "jdbc:sqlite" + ":data.db";
public static void main(String[] args) {
try{
Connection conn = DriverManager.getConnection(DB_URI);
Statement statement = conn.createStatement();
the question is, will it be cross platform if i will deploy my application with this DB?
Upvotes: 1
Views: 403
Reputation: 5394
You can use the user home directory. You are sure it's always defined whatever platform you deploy your program on, and your program will have read/write access to it.
Something like this:
String dbUri = "jdbc:sqlite:" + System.getProperty("user.home") + "/data.db";
Connection conn = DriverManager.getConnection(dbUri);
Upvotes: 1