【问题标题】:How to convert Azure DB URL to localhost DB URL?如何将 Azure DB URL 转换为 localhost DB URL?
【发布时间】:2019-10-06 08:21:02
【问题描述】:

我有一个通过 JDBC 连接到的 Azure SQL Server 数据库,但想要连接到我的 SQL Server“localhost”。在 SSMS 中,我无需密码即可连接到 localhost。那么,我还需要在 Java 中输入密码吗?

我有这样的代码:

 String connectionUrl =
                "jdbc:sqlserver://etcetc.database.windows.net:1433;"
                        + "database=med;"
                        + "user=windersan@salemimed;"
                        + "password=********;"
                        + "encrypt=true;"
                        + "trustServerCertificate=false;"
                       // + "hostNameInCertificate=*.database.windows.net;"
                        + "loginTimeout=30;";

如何更改它以连接到本地主机?

【问题讨论】:

    标签: java sql-server jdbc


    【解决方案1】:

    只需将 etcetc.database.windows.net 替换为 localhost 并将端口号 1433 替换为您正在使用的端口号。

    我使用了SQLServerDataSource 类来简化工作。您还可以创建一个字符串 URL 并将其设置在 DriverManger.getConnection() 中。

    试试这个代码:

    SQLServerDataSource dataSource = new SQLServerDataSource();  
    dataSource.setUser("windersan@salemimed");  
    dataSource.setPassword("********");  
    dataSource.setServerName("localhost");
    // set the port number of your system below.  
    dataSource.setPortNumber(1433); 
    dataSource.setDatabaseName("med"); 
    dataSource.setEncrypt(true);
    dataSource.setHostNameInCertificate("*.database.windows.net");
    dataSource.setTrustServerCertificate(false);
    Connection connection = dataSource.getConnection(); 
    

    请参阅下面的链接了解更多信息。

    1. Microsoft Docs - ISQLServerDataSource Interface - 这包含可用于设置数据源中各种属性的方法列表。

    2. Microsoft Docs - How to work with the connection - 这包含连接到 SQL Server 数据库的可能方式的示例。

    【讨论】:

    • @DevinAndresSalemi 这对您有帮助吗?
    【解决方案2】:

    连接字符串的第一行包含 url etcetc.database.windows.net:1433 这是数据库服务器的位置,您应该更改的位。

    此外,可能值得在 google 搜索关于使用 JDBC 连接到 SqlServer 以查看是否有任何示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2015-08-25
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多