The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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:涵盖以下主题:
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 如果Java平台不支持设置的URL类型,请使用setString
method.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, 该行由标识URL、DOCUMENT_NAME
and the URL itself, 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(); } }
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 如果Java平台不支持getObject
or getURL
is not supported by the Java platform, retrieve the URL as a String
object by calling the method getString
.getObject
或getURL
方法返回的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 如果需要代理服务器连接到Internet,openConnection
method accepts a java.net.Proxy
object as an argument. 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);