使用数据链接对象

DATALINK值通过 URL 引用基础数据源之外的资源。 URL(统一资源定位符)是指向 www 上资源的指针。资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库或搜索引擎的查询。

涵盖以下主题:

存储对外部数据的引用

使用方法PreparedStatement.setURL为准备好的语句指定java.net.URL对象。如果 Java 平台不支持所设置的 URL 类型,请使用setString方法存储 URL。

例如,假设 The Coffee Break 的所有者想要在数据库表中存储重要 URL 的列表。下面的示例DatalinkSample.addURLRow将一行数据添加到表DATA_REPOSITORY中。该行由标识 URL DOCUMENT_NAME和 URL 本身URL的字符串 组成:

public void addURLRow(String description, String url)
    throws SQLException {

    PreparedStatement pstmt = null;

    try {
        pstmt = this.con.prepareStatement(
            "INSERT INTO data_repository" +
            "(document_name,url) VALUES (?,?)");

        pstmt.setString(1, description);
        pstmt.setURL(2,new URL(url));
        pstmt.execute();
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        ex.printStackTrace();
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}

检索对外部数据的引用

使用方法ResultSet.getURL检索对外部数据的引用作为java.net.URL对象。如果 Java 平台不支持方法getObjectgetURL返回的 URL 类型,请通过调用方法getString将 URL 作为String对象检索。

以下示例DatalinkSample.viewTable显示表DATA_REPOSITORY中存储的所有 URL 的内容:

public static void viewTable(Connection con, Proxy proxy)
    throws SQLException, IOException {

    Statement stmt = null;
    String query =
      "SELECT document_name, url " +
      "FROM data_repository";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if ( rs.next() )  {
            String documentName = null;
            java.net.URL url = null;

            documentName = rs.getString(1);

            // Retrieve the value as a URL object.
            url = rs.getURL(2);

            if (url != null) {

                // Retrieve the contents
                // from the URL
          
                URLConnection myURLConnection =
                    url.openConnection(proxy);
                BufferedReader bReader =
                    new BufferedReader(
                        new InputStreamReader(
                            myURLConnection.
                                getInputStream()));

                System.out.println("Document name: " + documentName);

                String pageContent = null;

                while ((pageContent = bReader.readLine()) != null ) {
                    // Print the URL contents
                    System.out.println(pageContent);
                }
            } else {
                System.out.println("URL is null");
            }
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } catch(IOException ioEx) {
        System.out.println("IOException caught: " + ioEx.toString());
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        ex.printStackTrace();
    } finally {
        if (stmt != null) { stmt.close(); }
    }
}

示例DatalinkSample将 Oracle URL http://www.oracle.com存储在表DATA_REPOSITORY中。之后,它将显示DATA_REPOSITORY中存储的 URL 所引用的所有文档的内容,其中包括 Oracle 主页http://www.oracle.com

该示例使用以下语句从结果集中作为java.net.URL对象检索 URL:

url = rs.getURL(2);

该示例使用以下语句访问URL对象引用的数据:

URLConnection myURLConnection = url.openConnection(proxy);
BufferedReader bReader = new BufferedReader(
    new InputStreamReader(
        myURLConnection.getInputStream()));

System.out.println("Document name: " + documentName);

String pageContent = null;

while ((pageContent = bReader.readLine()) != null ) {
    // Print the URL contents
    System.out.println(pageContent);
}

方法URLConnection.openConnection不能接受任何参数,这意味着URLConnection表示与 Internet 的直接连接。如果需要代理服务器连接到 Internet,则openConnection方法接受java.net.Proxy对象作为参数。以下语句演示了如何使用服务器名www-proxy.example.com和端口号80创建 HTTP 代理:

Proxy myProxy;
InetSocketAddress myProxyServer;
myProxyServer = new InetSocketAddress("www-proxy.example.com", 80);
myProxy = new Proxy(Proxy.Type.HTTP, myProxyServer);