Java-如何使用我的项目文件在同一文件夹中重新编译Sqlite数据库?

时间:2021-12-30 20:44:06

am working on a certain java Management System where am using sqlite as my primary database. Am using netbeans gui builder to work on my project. I have googled and stack overflowed how to reference sqlite database in the same dir with my project files without including full path like c://filepath but nothing promising yet. I don't want to include the full path to my database, I want to use sqlite file from the project files so that everything will work smoothly even after distributing my project...

我正在使用某个java管理系统,其中使用sqlite作为我的主数据库。我使用netbeans gui builder来处理我的项目。我google搜索和堆栈溢出如何在我的项目文件的同一个目录中引用sqlite数据库,而不包括像c:// filepath这样的完整路径,但没有任何希望。我不想包含我的数据库的完整路径,我想使用项目文件中的sqlite文件,以便即使在分发我的项目之后一切都能顺利运行...

My Connection Class currently Looks like this;

My Connection Class目前看起来像这样;

import java.sql.Connection;
import java.sql.DriverManager;

public class Connect {
    private static Connection con = null; 

    public static Connection connecting(){

        try{
            Class.forName("org.sqlite.JDBC"); 
            con = DriverManager.getConnection("jdbc:sqlite:programming.sqlite");
            JOptionPane.showMessageDialog(null, "Working", null, JOptionPane.INFORMATION_MESSAGE);

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e.getMessage(), null, JOptionPane.ERROR_MESSAGE); 
        }
        return con; 
    }

}

However that's seems not to work.. Remember Connection class and database are saved in the same dir...

然而,这似乎不起作用..记住连接类和数据库保存在同一个目录中...

Please note am avoiding absolute path like c://users/programming.sqlite

请注意我避免使用像c://users/programming.sqlite这样的绝对路径

Anything to help please???

有什么可以帮忙的吗???

1 个解决方案

#1


3  

I think it must work as you shown, but, you can load programming.sqlite as a java.io.File and then put absolute path into jdbc url connection. I mean this:

我认为它必须像您所示的那样工作,但是,您可以将programming.sqlite作为java.io.File加载,然后将绝对路径放入jdbc url连接。我的意思是:

File temp = new File("programming.sqlite");
String connection = "jdbc:sqlite:" + temp.getAbsolutePath().replace("\\","\\\\");
con = DriverManager.getConnection(connection);
...

If this code doesn't work for you, then your problem must be with programming.sqlite location, because in this case it is not on the same home dir of your java programm.

如果此代码不适合您,那么您的问题必须在programming.sqlite位置,因为在这种情况下它不在您的java程序的同一个主目录上。

#1


3  

I think it must work as you shown, but, you can load programming.sqlite as a java.io.File and then put absolute path into jdbc url connection. I mean this:

我认为它必须像您所示的那样工作,但是,您可以将programming.sqlite作为java.io.File加载,然后将绝对路径放入jdbc url连接。我的意思是:

File temp = new File("programming.sqlite");
String connection = "jdbc:sqlite:" + temp.getAbsolutePath().replace("\\","\\\\");
con = DriverManager.getConnection(connection);
...

If this code doesn't work for you, then your problem must be with programming.sqlite location, because in this case it is not on the same home dir of your java programm.

如果此代码不适合您,那么您的问题必须在programming.sqlite位置,因为在这种情况下它不在您的java程序的同一个主目录上。