简体   繁体   English

apache tomcat 在 Z93F725A07423FE1C8489F448B33D2 中创建 sqlite 数据库的新实例

[英]apache tomcat creating new instance of sqlite database in java

I am using an sqlite database in java using xerial sqlite jdbc driver in order to store some data, 1 connection at a time. I am using an sqlite database in java using xerial sqlite jdbc driver in order to store some data, 1 connection at a time. It worked fine until I decided to use Apache tomcat on my server instead of java sockets (used for testing) and I noticed my queries yielded null results despite data being on the database. It worked fine until I decided to use Apache tomcat on my server instead of java sockets (used for testing) and I noticed my queries yielded null results despite data being on the database. I searched this issue and apparently tomcat creates a new instance of this database, which has the same tables but has no data.我搜索了这个问题,显然 tomcat 创建了这个数据库的一个新实例,它具有相同的表但没有数据。 I tried moving my database into the resource folder but this did not help.我尝试将我的数据库移动到资源文件夹中,但这没有帮助。

public static Connection getConn() throws SQLException {
      if(c == null){
         c = DriverManager.getConnection("jdbc:sqlite:database.db");
      }
      return c;
   }

this is how the code connects to the database.这就是代码连接到数据库的方式。 I believe moving the database and changing the address should solve the issue but I dont know where to.我相信移动数据库和更改地址应该可以解决问题,但我不知道去哪里。 thanks!谢谢!

You should be able to use the full path to your existing database file in your JDBC URL - so, something like:您应该能够在 JDBC URL 中使用现有数据库文件的完整路径 - 所以,类似于:

"jdbc:sqlite:/path/to/your/mysqlite.db"

If you are on Windows, then it might look like this:如果您使用的是 Windows,那么它可能如下所示:

"jdbc:sqlite:C:/path/to/your/mysqlite.db"

These are examples of absolute paths.这些是绝对路径的示例。

In your case, the database location in your JDBC URL is simply mysqlite.db , with no additional path information.在您的情况下,您的 JDBC URL 中的数据库位置只是mysqlite.db ,没有其他路径信息。 This is a relative path, because it does not start with a slash or a drive letter.这是一个相对路径,因为它不以斜杠或驱动器号开头。

This means the location of the DB file is relative to the working directory of the Java program (where the program was started).这意味着 DB 文件的位置相对于 Java 程序的工作目录(程序启动的地方)。 In the case of Tomcat, that is typically the CATALINA_HOME/bin directory - where CATALINA_HOME is typically the location where Tomcat was installed.对于 Tomcat,这通常是CATALINA_HOME/bin目录 - 其中CATALINA_HOME通常是安装 Tomcat 的位置。 (This may vary depending on how Tomcat was installed and launched.) (这可能因 Tomcat 的安装和启动方式而异。)

I would recommend you keep the database file in a separate location from Tomcat.我建议您将数据库文件保存在与 Tomcat 不同的位置。

(SQLite will create a new, empty database in the specified location, if one does not already exist - so it's not Tomcat which is creating it, but rather the SQLite driver). (SQLite 将在指定位置创建一个新的空数据库,如果该数据库尚不存在 - 所以创建它的不是 Tomcat,而是 SQLite 驱动程序)。

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

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