5

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Izbitzer
  • 53
  • 1
  • 1
  • 3

2 Answers2

6

Does this work?

String jdbcString = "jdbc:sqlserver://" + hostname + ":" + port + ";databaseName={Database Name}";
Tarski
  • 5,360
  • 4
  • 38
  • 47
0

You should use the following syntax:

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

Example:

jdbc:sqlserver://localhost:1433;DataBaseName=testDB
nKn
  • 13,691
  • 9
  • 45
  • 62
Ismail
  • 1,668
  • 1
  • 15
  • 10