'format for Using Sqlite Database (from Java Package) to the Java Code inside `driverManager` to make connection

I have inserted my SQLite.db file inside of package named newpackage Location of database file I inserted here isCollegeProject\src\operalogsapp\newpackage\SQLite.db Now I want to get this database, so I want the correct format for inserting it to driver manager.

public class databaseConnection {
public static Connection con;
public static Connection getDBConnection(String username, String password, Integer portNumber, String serviceName){ 
    try {
        //Register the JDBC driver
       System.out.println("before className"); 
        Class.forName("org.sqlite.JDBC");
        System.out.println(con==null);
        System.out.println("Registered to the JDBC driver");
        
        //Open the connection
        if("V50700_HOTEL".equals(username) && "V50700_HOTEL".equals(password) && portNumber==1501 && "operal".equals(serviceName)){
            con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\absasahu\\Documents\\db\\SQLite.db");

           //inside the getConnection (above line) I want to get correct location of database to enter.
            System.out.println("DriverManager connected to db");
        }

    } catch (ClassNotFoundException | SQLException ex) {
        System.out.println("Exception : "+ex);
        //Logger.getLogger(dbCoonectionCode.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("DFHUDSBFSDF");
    return con;  
}

Any help will be appreciated.



Solution 1:[1]

"jdbc:sqlite:sqlite_database_file_path", in your case from your code:

"jdbc:sqlite:C:/Users/absasahu/Documents/db/SQLite.db"

Regarding your question using a relative path:

"jdbc:sqlite:SQLite.db"

See also the answers at Set path to a jdbc SQLite database, where it will be located

A complete example with class file and driver in the same directory (unfortunately on a unix machine):

// mkdir collegeproject
// cd collegeproject
// curl -LJO https://github.com/xerial/sqlite-jdbc/releases/download/3.36.0.3/sqlite-jdbc-3.36.0.3.jar
// javac SqliteTest.java
// java -classpath ".;sqlite-jdbc-3.36.0.3.jar" SqliteTest  # in Windows
// java -classpath ".:sqlite-jdbc-3.36.0.3.jar" SqliteTest  # in Unix

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqliteTest {
    public static void main(String[] args)
    {
        try (Connection connection = DriverManager.getConnection("jdbc:sqlite:sqltest.db");) {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("drop table if exists book");
                statement.executeUpdate("create table book (id integer, title string)");
                statement.executeUpdate("insert into book values(1, 'SQLite with JDBC for Beginners')");
                try (ResultSet rs = statement.executeQuery("select * from book")) {
                    while (rs.next()) {
                        System.out.println("id = " + rs.getInt("id"));                    
                        System.out.println("title = " + rs.getString("title"));
                    }
                } catch (SQLException e) {
                    System.err.println(e.getMessage());
                }
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
    }
}
$ javac SqliteTest.java                                  
$ java -classpath ".:sqlite-jdbc-3.36.0.3.jar" SqliteTest
id = 1
title = SQLite with JDBC for Beginners
$ 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1