简体   繁体   中英

SQL error coming while trying to access SQLite.db

I am trying to access my SQLite database which is in a Java package. When I am giving it's absolute path then it's OK, but when I am putting my db file inside java package then it isn't working. Error:

[SQLITE_ERROR] SQL error or missing database (no such table: FinalProjectDemo)

Picture of directory is as in this picture here. https://i.stack.imgur.com/kH82o.png

My code to db connection is given below:

package operalogsapp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
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==1521 && "opera".equals(serviceName)){
//                con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\absasahu\\Documents\\db\\sqlite.db");
                   con=DriverManager.getConnection("jdbc:sqlite:SQLite.db");
// here in this line i am going wrong.
                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;  
    }   
}

I tried these ways but they don't work.

"jdbc:sqlite:C:\\Users\\absasahu\\Documents\\db\\sqlite.db"
// this works only when my db file is in this location

Note: I want my project to export in the jar file. So, want to include DB inside. Please help me out with suggestions.

A SQLite database cannot reside inside a package (I assume you actually mean inside a jar file) because it would not be writable. You can distribute an initial database inside a jar but then you first have to copy it out of the jar into some location where it is writable when you lauch the application for the first time.

In the picture you included your file likely isn't in the working directory. On most IDE's in the "Run Configurations" menu you will be able to see what your working directory is. Usually it's going to be the root folder for your project.

例子

If you put your sqlite.db folder into the working directory you should be able to reference it as shown in your code.

My problem is resolved. I had put my SQLite.DB file inside the src folder. So whenever I put a URL like this "jdbc:sqlite:SQLite.db" then it was checking it on the C:\Users\absasahu\Documents\GitHub\CollegeProject directory, and it has also an SQLite.db but without any table. So, the compiler was checking this directory first, so it was showing this error. What did I do? I replaced this empty DB with a filled DB, and then this problem was resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM