文档

Java™ 教程-Java Tutorials 中文版
使用数据链接对象
Trail: JDBC(TM) Database Access
Lesson: JDBC Basics

使用数据链接对象

A DATALINK value references a resource outside the underlying data source through a URL. A URL, uniform resource locator, is a pointer to a resource on the World Wide Web. 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.

涵盖以下主题:

Storing References to External Data

Use the method PreparedStatement.setURL to specify a java.net.URL object to a prepared statement. In cases where the type of URL being set is not supported by the Java platform, store the URL with the setString method.

For example, suppose the owner of The Coffee Break would like to store a list of important URLs in a database table. The following example, DatalinkSample.addURLRow adds one row of data to the table DATA_REPOSITORY. The row consists of a string identifying the URL, DOCUMENT_NAME and the URL itself, 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();
        }
    }
}

Retrieving References to External Data

Use the method ResultSet.getURL to retrieve a reference to external data as a java.net.URL object. 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.

The following example, DatalinkSample.viewTable, displays the contents of all the URLs stored in the table DATA_REPOSITORY:

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

The sample DatalinkSample stores the Oracle URL, http://www.oracle.com in the table 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, http://www.oracle.com.

The sample retrieves the URL from the result set as a java.net.URL object with the following statement:

url = rs.getURL(2);

The sample accesses the data referred to by the URL object with the following statements:

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. If you require a proxy server to connect to the Internet, the openConnection method accepts a java.net.Proxy object as an argument. The following statements demonstrate how to create an HTTP proxy with the server name www-proxy.example.com and port number 80:

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