JDBC URL with database containing spaces

I am trying to connect to a SQL Server database using JDBC, the database I am trying to connect to contains a space, and unfortunately I have no control over the name, so I cannot change it.

The code I use is:

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

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

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

but it doesn’t work either.

The error message I gee:

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

I am using the latest Microsoft JDBC driver.

+4
source share
2 answers

It works?

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

You should use the following syntax:

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

Example:

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

Source: https://habr.com/ru/post/1314201/


All Articles