Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC url with database containing spaces

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.

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:

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.

like image 382
Izbitzer Avatar asked Jun 29 '10 13:06

Izbitzer


1 Answers

Does this work?

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName={Database Name}";
like image 93
Tarski Avatar answered Sep 28 '22 23:09

Tarski