Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate连接字符串do MS SQL Server 2008数据库名称(带大括号)_Java_Hibernate_Sql Server 2008_Java 8_Connection String - Fatal编程技术网

Java Hibernate连接字符串do MS SQL Server 2008数据库名称(带大括号)

Java Hibernate连接字符串do MS SQL Server 2008数据库名称(带大括号),java,hibernate,sql-server-2008,java-8,connection-string,Java,Hibernate,Sql Server 2008,Java 8,Connection String,我必须连接到数据库,它的名称中有大括号-{和}(谁能做到?!)。 因此,数据库名称类似于Production{guid-part-123123-123123-abcd},我在尝试连接它时出错 连接字符串为: public synchronized static SessionFactory getSessionFactory(String dbName) { String url = String.format("jdbc:sqlserver://someServer:1433

我必须连接到数据库,它的名称中有大括号-
{
}
(谁能做到?!)。 因此,数据库名称类似于
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/连接字符串?连接到此服务器上的其他数据库可以正常工作,但如果数据库名称包含“{}”,则会失败。

最后,我设法解决了这个问题,它非常简单,但花了几天时间才解决。选中此项:

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
作为属性传递,这意味着未解析。找到解决方案帮助我回答了这个问题: