包含空格的数据库的JDBC URL

时间:2020-12-24 16:48:44

I'm trying to connect to a SQL Server database using JDBC, the database I'm trying to connecto to contains a space, and unfortunately I have no control over the name, so I can't change it.

我正在尝试使用JDBC连接到SQL Server数据库,我正在尝试连接的数据库包含一个空格,不幸的是我无法控制名称,因此我无法更改它。

The code I'm using is:

我正在使用的代码是:

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName=Database Name";
    try {
        connection = DriverManager.getConnection(jdbcString, username, password);
    }

I've also tried following the instructions on this link: http://msdn.microsoft.com/en-us/library/ms378428%28SQL.90%29.aspx by haveing the space inside braces:

我也试过按照这个链接上的说明:http://msdn.microsoft.com/en-us/library/ms378428%28SQL.90%29.aspx通过括号内的空格:

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName=Database{ }Name";

but that doesn't seem to work either.

但这似乎也不起作用。

The error message I gee is:

我的错误消息是:

ERROR: Couldn't connect to the database: The connection string contains a badly formed name or value.

I'm using the latest JDBC driver from Microsoft.

我正在使用Microsoft的最新JDBC驱动程序。

2 个解决方案

#1


6  

Does this work?

这有用吗?

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName={Database Name}";

#2


0  

You should use the following syntax:

您应该使用以下语法:

jdbc:sqlserver://"your Server Name":1433;DataBaseName="Data Base Name"

Example:

jdbc:sqlserver://localhost:1433;DataBaseName=testDB

#1


6  

Does this work?

这有用吗?

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName={Database Name}";

#2


0  

You should use the following syntax:

您应该使用以下语法:

jdbc:sqlserver://"your Server Name":1433;DataBaseName="Data Base Name"

Example:

jdbc:sqlserver://localhost:1433;DataBaseName=testDB