gpt4 book ai didi

java - 如何在persistence.xml中指定相对于应用程序文件夹的jdbc.url?

转载 作者:行者123 更新时间:2023-11-30 05:10:46 25 4
gpt4 key购买 nike

一旦我使用 JPA 部署应用程序,用户就会选择将其安装在某个地方。然而,属性设置为:

<property name="javax.persistence.jdbc.url" value="jdbc:derby:db;create=true"/>

被解释为以下异常:无法在\db 中创建数据库。在整个开发过程中,它曾经是项目文件夹的相对路径,而不是现在的根目录。我应该怎么做才能使路径保持相对于安装应用程序的文件夹的路径?或者更糟糕的是,userdir。

最佳答案

您应该在某处写入安装位置,并在创建连接之前将 derby.system.home 系统属性设置为此位置。引用Using Java DB in Desktop Applications文章:

Connecting to the Java DB Database

...

All connection URLs have the following form:

jdbc:derby:<dbName>[propertyList]

The dbName portion of the URL identifies a specific database. A database can be in one of many locations: in the current working directory, on the classpath, in a JAR file, in a specific Java DB database home directory, or in an absolute location on your file system. The easiest way to manage your database location in an embedded environment is to set the derby.system.home system property. This property tells Java DB the default home location of all databases. By setting this property, the Address Book demo ensures that Java DB always finds the correct application database. The application database is named DefaultAddressBook, and it will exist within the directory indicated by the derby.system.home property. The connection URL for this database would look like this:

jdbc:derby:DefaultAddressBook

...

To connect to the DefaultAddressBook database, the demo must first set the derby.system.home system property. The demo uses the .addressbook subdirectory of the user's home directory. Use the System class to find out the user's home directory. Then use the class again to set the derby.system.home property:

private void setDBSystemDir() {
// Decide on the db system directory: <userhome>/.addressbook/
String userHomeDir = System.getProperty("user.home", ".");
String systemDir = userHomeDir + "/.addressbook";

// Set the db system directory.
System.setProperty("derby.system.home", systemDir);
}

关于java - 如何在persistence.xml中指定相对于应用程序文件夹的jdbc.url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3430723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com