首页 > 解决方案 > 使用 Selenium 连接到数据库时出现未知主机异常

问题描述

当我连接到客户端网络(使用 VPN)时,我可以成功地从数据库中获取数据。但是,如果我断开与客户端网络的连接并在本地运行相同的功能,那么我将遇到错误 -

连接失败:SAP DBTech JDBC:无法连接到 jdbc:sap://saphsg.XXXX.XX.com:30015 [未知主机 saphsg.XXXX.XXX.com:30015 [null],-709]。

下面是我正在使用的代码 -

public static void dbConnection(String query) throws ClassNotFoundException {
        Connection connection = null;
        try {                  
            Class.forName("com.sap.db.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:sap://saphsg.XXXX.XXX.com:30015?encrypt=true&validateCertificate=false", UserID, Password);                             
        
        } catch (SQLException e) {
            System.err.println("Connection Failed:" +e.getMessage());
        }
        if (connection != null) {
            try {
                System.out.println("Connection to HANA successful!");
                Statement stmt = connection.createStatement();
                ResultSet resultSet = stmt.executeQuery(query);
                while (resultSet.next()){
                    String Name1 = resultSet.getString(1);                                      
                    String Name2 = resultSet.getString(4);  
                    String Name3 = resultSet.getString(5);  
                    System. out.println(Name1+"----"+Name2+"----"+Name3);       
                }       
            } catch (SQLException e) {
                System.err.println("Query failed!");
            }
        }
    }

标签: javadatabaseselenium-webdriverautomationssl-certificate

解决方案


推荐阅读