[英]Hibernate connection string do MS SQL Server 2008 database name with braces
I have to connect to the database, which has braces - {
and }
in the name (who could do that?!).我必须连接到数据库,它的名称中有大括号 -
{
和}
(谁能做到?!)。 So the database name is like Production{guid-part-123123-123123-abcd}
and I got errors when I try to connent it.所以数据库名称就像
Production{guid-part-123123-123123-abcd}
,当我尝试连接它时出现错误。
Connection string is:连接字符串是:
public synchronized static SessionFactory getSessionFactory(String dbName) {
String url = String.format("jdbc:sqlserver://someServer:1433;databaseName=%s", dbName); // what can I do to give proper database name?
return new Configuration().configure()
.setProperty("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.setProperty("hibernate.default_schema", "dbo")
.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect")
.setProperty("hibernate.connection.username", "admin")
.setProperty("hibernate.connection.password", "admin")
.setProperty("hibernate.connection.url", url)
.buildSessionFactory();
}
How to build right url/connection string?如何构建正确的 url/连接字符串? Connection to the other databases on this server works, but if database name contains '{}' it fails.
连接到此服务器上的其他数据库有效,但如果数据库名称包含“{}”,则连接失败。
Finally I've managed to solve this problem, it's very simple but it took days to figure it out.最后我设法解决了这个问题,这很简单,但花了几天时间才弄明白。 Check this:
检查这个:
public synchronized static SessionFactory getSessionFactory(String dbName) {
String url = "jdbc:sqlserver://someServer:1433"; // we should skip here database name
return new Configuration().configure()
.setProperty("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.setProperty("hibernate.default_schema", "dbo")
.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect")
.setProperty("hibernate.connection.username", "admin")
.setProperty("hibernate.connection.password", "admin")
.setProperty("hibernate.connection.url", url) // url is parsed by DriverManager class
.setProperty("hibernate.connection.databaseName", dbName) // this property is not parsed but just used
.buildSessionFactory();
}
As you can see, to solve it you need to pass the dbName
as property and that means is not parsed.如您所见,要解决它,您需要将
dbName
作为属性传递,这意味着不会被解析。 To find a solution helped me this answer: https://stackoverflow.com/a/53704163/1679995找到解决方案帮助我回答这个问题: https : //stackoverflow.com/a/53704163/1679995
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.