Documentation

The Java™ Tutorials
Hide TOC
Using Datalink Objects使用Datalink对象
Trail: JDBC Database Access
Lesson: JDBC Basics

Using Datalink Objects使用数据链接对象

A DATALINK value references a resource outside the underlying data source through a URL. DATALINK值通过URL引用基础数据源之外的资源。A URL, uniform resource locator, is a pointer to a resource on the World Wide Web. URL(一致性资源定位器)是指向万维网上资源的指针。A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库或搜索引擎的查询。

The following topics are covered:涵盖以下主题:

Storing References to External Data存储对外部数据的引用

Use the method PreparedStatement.setURL to specify a java.net.URL object to a prepared statement. 使用方法PreparedStatement.setURL为准备好的语句指定java.net.URL对象。In cases where the type of URL being set is not supported by the Java platform, store the URL with the setString method.如果Java平台不支持设置的URL类型,请使用setString方法存储URL。

For example, suppose the owner of The Coffee Break would like to store a list of important URLs in a database table. 例如,假设咖啡休息时间的所有者希望在数据库表中存储重要URL的列表。The following example, DatalinkSample.addURLRow adds one row of data to the table DATA_REPOSITORY. 下面的示例DatalinkSample.addURLRow将一行数据添加到表DATA_REPOSITORY中。The row consists of a string identifying the URL, DOCUMENT_NAME and the URL itself, URL:该行由标识URL、DOCUMENT_NAME和URL本身的字符串URL组成:

public void addURLRow(String description, String url) throws SQLException {
    String query = "INSERT INTO data_repository(document_name,url) VALUES (?,?)";
    try (PreparedStatement pstmt = this.con.prepareStatement(query)) {
      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();
    }
  }

Retrieving References to External Data检索对外部数据的引用

Use the method ResultSet.getURL to retrieve a reference to external data as a java.net.URL object. 使用ResultSet.getURL方法以java.net.URL对象的形式检索对外部数据的引用。In cases where the type of URL returned by the methods getObject or getURL is not supported by the Java platform, retrieve the URL as a String object by calling the method getString.如果Java平台不支持getObjectgetURL方法返回的URL类型,请通过调用getString方法将URL作为String对象检索。

The following example, DatalinkSample.viewTable, displays the contents of all the URLs stored in the table DATA_REPOSITORY:以下示例DatalinkSample.viewTable显示存储在表DATA_REPOSITORY中的所有URL的内容:

public static void viewTable(Connection con, Proxy proxy)
    throws SQLException, IOException {
    String query = "SELECT document_name, url FROM data_repository";
    try (Statement 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();
    }
  }

The sample DatalinkSample stores the Oracle URL, https://www.oracle.com in the table DATA_REPOSITORY. 示例DatalinkSample存储Oracle URL,https://www.oracle.com在表DATA_REPOSITORY中。Afterward, it displays the contents of all documents referred to by the URLs stored in DATA_REPOSITORY, which includes the Oracle home page, https://www.oracle.com.之后,它将显示存储在DATA_REPOSITORY(包括Oracle主页https://www.oracle.com)中的URL引用的所有文档的内容。

The sample retrieves the URL from the result set as a java.net.URL object with the following statement:此示例使用以下语句从结果集中检索作为java.net.URL对象的URL:

url = rs.getURL(2);

The sample accesses the data referred to by the URL object with the following statements:示例使用以下语句访问URL对象引用的数据:

// 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);
}

The method URLConnection.openConnection can take no arguments, which means that the URLConnection represents a direct connection to the Internet. 方法URLConnection.openConnection不能接受任何参数,这意味着URLConnection表示到Internet的直接连接。If you require a proxy server to connect to the Internet, the openConnection method accepts a java.net.Proxy object as an argument. 如果需要代理服务器连接到Internet,openConnection方法将接受java.net.Proxy对象作为参数。The following statements demonstrate how to create an HTTP proxy with the server name www-proxy.example.com and port number 80:以下语句演示如何使用服务器名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);

Previous page: Using Customized Type Mappings
Next page: Using RowId Objects