Hibernate connection string do MS SQL Server 2008 带大括号的数据库名
Hibernate connection string do MS SQL Server 2008 database name with braces
我必须连接到名称中有大括号 {
和 }
的数据库(谁能做到?!)。
所以数据库名称类似于 Production{guid-part-123123-123123-abcd}
,当我尝试连接它时出现错误。
连接字符串是:
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();
}
如何构建正确的url/connection字符串?连接到此服务器上的其他数据库有效,但如果数据库名称包含“{}”,则连接失败。
终于解决了这个问题,很简单,但是弄了好几天。检查这个:
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();
}
如您所见,要解决它,您需要将 dbName
作为 属性 传递,这意味着未解析。找到解决方案帮助了我这个答案:
我必须连接到名称中有大括号 {
和 }
的数据库(谁能做到?!)。
所以数据库名称类似于 Production{guid-part-123123-123123-abcd}
,当我尝试连接它时出现错误。
连接字符串是:
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();
}
如何构建正确的url/connection字符串?连接到此服务器上的其他数据库有效,但如果数据库名称包含“{}”,则连接失败。
终于解决了这个问题,很简单,但是弄了好几天。检查这个:
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();
}
如您所见,要解决它,您需要将 dbName
作为 属性 传递,这意味着未解析。找到解决方案帮助了我这个答案: