如何获取数据库的realative路径? [英] How to get realative path for database?

查看:148
本文介绍了如何获取数据库的realative路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个jframe,通过使用textfields和all来将一些数据存储在数据库中。我有以下数据库连接代码。我正在使用sqlite数据库。

I am developing a jframe to store some data in database by using textfields and all. i have following code for database connection. I am using sqlite database.

**Connectdatabase.java**

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package shreesai;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import javax.swing.JOptionPane;

    /**
     *
     * @author DeepRocks
     */
    public class Connectdatabase {

        Connection con = null;

        public static Connection ConnecrDb(){

            try{
                Class.forName("org.sqlite.JDBC");
                Connection con = DriverManager.getConnection("jdbc:sqlite:G:\\Development\\Project\\database\\shreesai.sqlite");
                return con;
            }
            catch(ClassNotFoundException | SQLException e){
                JOptionPane.showMessageDialog(null,"Problem with connection of database");
                return null;
            }
        }
}****

/ *所有工作都很精细,但我想要的是,如果我将这个项目导出到JAR,我的客户将从不同的路径运行JAR ???? * /

我尝试过使用此代码

public class Connectdatabase {

    Connection con = null;

    public static Connection ConnecrDb(){

        try{
            String dir = System.getProperty("user.dir");
            Class.forName("org.sqlite.JDBC");
            Connection con = DriverManager.getConnection("jdbc:sqlite:"+dir+"shreesai.sqlite");
            return con;
        }
        catch(ClassNotFoundException | SQLException e){
            JOptionPane.showMessageDialog(null,"Problem with connection of database");
            return null;
        }
    }

}

即使我试过这个:

public class Connectdatabase {

        Connection con = null;

        public static Connection ConnecrDb(){

            try{
                String dir = System.getProperty("user.dir");
                String maindir = dir + File.separator + File.separator ;
                Class.forName("org.sqlite.JDBC");
                Connection con = DriverManager.getConnection("jdbc:sqlite:"+maindir+"shreesai.sqlite");
                return con;
            }
            catch(ClassNotFoundException | SQLException e){
                JOptionPane.showMessageDialog(null,"Problem with connection of database");
                return null;
            }
        }

    }

NOTHING 我认为双反斜杠必然会出现问题导致dir返回: G:\Development\Project \database 。我们想要G:\\Development\\Project \\database \\

But NOTHING i think there must be problem with that double backslashes cause dir returning : G:\Development\Project\database. and we want G:\\Development\\Project\\database\\

任何帮助?

推荐答案

你尝试的方式没有错,但它只适用于你的计算机,因此,解决方案很简单,带上 .sqlite 文件在 src 里面,也许你想创建一个名为 Database 的包,它是不是必需的,但它更好。

The ways you're tried are not wrong, but it's right only on your computer, so, the solution is easy, bring the .sqlite file inside the src, maybe you want create a package named Database, it's not required, but it's better.

如果 .sqlite 文件在源内部就是这样:

IF the .sqlite file was inside the source would be like that:

src / shreesai.sqlite

.... = DriverManager.getConnection(jdbc:sqlite:src / shreesai.sqlite);

不要使用绝对路径,并且由于窗口平台不同,避免对目录( \\ )使用反斜杠,请使用斜杠( / )而不是。

Don't want the absolute path too, and avoid using backslashes with directories( \\ ) because of different windowing platforms, use slash(/) instead.

这篇关于如何获取数据库的realative路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆