简体   繁体   English

如何连接到位于项目文件夹内的数据库

[英]How to connect to a database located inside project folder

Maybe it's a silly question, but I am new to databases and I don't know how to do this. 也许这是一个愚蠢的问题,但是我是数据库的新手,我也不知道该怎么做。

I have the following line of code that I use in order to connect to a database located in my D: drive. 我使用以下代码行来连接到位于D:驱动器中的数据库。 How can I change it so it connects to the database if the database is located inside the project folder? 如果数据库位于项目文件夹中,如何更改它以使其连接到数据库? (I use netbeans IDE) (我使用netbeans IDE)

Connection conn = DriverManager.getConnection("jdbc:sqlite:d:\\Databases\\DataBase1.db");
//I don't want an exact path, I need it so it works idependetly of where the project folder is located

According to this answer : 根据这个答案

In your code you can use a relative path and it should be relative to the top of your project's directory. 在您的代码中,您可以使用相对路径,它应该相对于项目目录的顶部。 In NetBeans' Databases Service you will need to either use an absolute path to connect to the database that exists (in your project) or you will need to provide a relative path... 在NetBeans的数据库服务中,您将需要使用绝对路径来连接到(在您的项目中)存在的数据库,或者您需要提供相对路径...

So according the SQLite Tutorial connection string should be as follows: 因此根据SQLite Tutorial的连接字符串应如下所示:

    jdbc:sqlite:DataBase1.db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取位于项目文件夹java中的文件 - How to read a file that is located in the project folder java 如何读取位于JSP Web应用程序中项目文件夹之一中的图像 - How to Read an Image located in one of the Project Folder in JSP Web Application 如何读取位于eclipse中java项目文件夹中的bin文件 - How to read bin file located in java project's folder in eclipse 在与项目目录中的 a.jar 文件相同的文件夹中创建一个文件 - Create a file inside the same folder as that of a .jar file which is located inside the project directory 如何执行位于Spring MVC项目中的脚本(可执行类)? - How to execute a script (an executable class) located inside a Spring MVC project? 如何连接到BeanFactoryPostProcessor内部的数据库? - How to connect to a database inside a BeanFactoryPostProcessor? 如何打开位于 java 中项目文件夹以外的文件夹中的文本文件(或任何文件) - how to open a text file(or any file) located in a folder other than the project folder in java 如何获取文件夹所在的驱动器? - How to get the located drive of a folder? 无法连接到服务器上的远程数据库 - unable to connect to remote database located on server 如何在项目文件夹中检索pdf文件? - how to retrieve pdf file inside project folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM