简体   繁体   English

带有文件路径上空格的JDBC连接字符串(sqlite,hsqldb)

[英]JDBC connection string with spaces on file path (sqlite, hsqldb)

I have to connect to a database that is hosted inside a file. 我必须连接到托管在文件中的数据库。 Instead of pointing to a port I have to point to a file like this sqlite connection string: 而不是指向一个端口我不得不指向像这个sqlite连接字符串的文件:

"jdbc:sqlite:database/users.db"

But I can't figure out how to specify the path when the path has spaces on it like: 但我无法弄清楚当路径上有空格时如何指定路径,如:

"jdbc:sqlite:C:\\Documents and Settings\\users.db"
"jdbc:sqlite:C:/Documents\ and\ Settings/users.db"

None of these work. 这些都不起作用。

Preferably I also want to be able to specify this path in a manner that is system agnostic (ie works both on windows and on linux). 我最好还希望能够以系统无关的方式指定此路径(即在Windows和Linux上都可以工作)。 Without needing specific processing on each. 无需对每个进行特定处理。

It turns out that Nishant was right, I don't need to escape characters or use backslashes at all. 事实证明,Nishant是对的,我根本不需要逃避角色或使用反斜杠。 This works: 这有效:

"jdbc:sqlite:C:/Documents and Settings/users.db"

I had actually tried that but another bug prevented me from seeing it working. 我实际上已经尝试过,但另一个错误阻止我看到它工作。 I had two bugs, but thought that I only had one. 我有两个错误,但我以为我只有一个。

I believe JDBC escaping is with {}. 我认为JDBC转义是{}。 So that would become 这将成为

"jdbc:sqlite:{C:/Documents and Settings/users.db}"

or 要么

"{jdbc:sqlite:C:/Documents and Settings/users.db}"

or a variation of that. 或其变体。

For the system independent separator you should use File.separator from the standard API: 对于独立于系统的分隔符,您应该使用标准API中的File.separator

File (Java SE 7) 文件(Java SE 7)

The system-dependent default name-separator character. 系统相关的默认名称分隔符。 This field is initialized to contain the first character of the value of the system property file.separator. 此字段初始化为包含系统属性file.separator的值的第一个字符。 On UNIX systems the value of this field is '/'; 在UNIX系统上,该字段的值为“/”; on Microsoft Windows systems it is '\\'. 在Microsoft Windows系统上它是'\\'。

File.separator returns this char as a String . File.separator将此char作为String返回。

我们也可以尝试双反斜杠,它对我有用

"jdbc:sqlite:C:\\Documents and Settings\\users.db"

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

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