1

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?

1
  • This might help. Commented May 21, 2019 at 15:30

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.