本Java教程是针对JDK 8编写的。本页面中描述的示例和实践不利用后续版本中引入的改进,并可能使用不再可用的技术。
有关Java SE 9及后续版本中更新的语言特性的摘要,请参阅Java语言变更。
有关所有JDK版本的新功能、增强功能以及已删除或弃用选项的信息,请参阅JDK发行说明。
DATALINK值通过URL引用底层数据源之外的资源。URL(统一资源定位符)是指向万维网上资源的指针。资源可以是简单的文件或目录,也可以是对更复杂对象的引用,例如对数据库查询或搜索引擎的引用。
以下主题已涵盖:
使用方法PreparedStatement.setURL来将java.net.URL对象指定给预编译语句。如果要设置的URL类型不受Java平台支持,请使用setString方法存储URL。
例如,假设The Coffee Break的所有者想要将重要的URL列表存储在数据库表中。下面的方法DatalinkSample.addURLRow将一行数据添加到DATA_REPOSITORY表中。该行由标识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();
}
}
使用方法ResultSet.getURL将对外部数据的引用作为java.net.URL对象检索出来。如果getObject或getURL方法返回的URL类型不受Java平台支持,请通过调用getString方法将URL作为String对象检索出来。
以下方法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);
// 将值作为URL对象检索
url = rs.getURL(2);
if (url != null) {
// 从URL检索内容
URLConnection myURLConnection = url.openConnection(proxy);
BufferedReader bReader =
new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
System.out.println("文档名称:" + documentName);
String pageContent = null;
while ((pageContent = bReader.readLine()) != null ) {
// 打印URL内容
System.out.println(pageContent);
}
} else {
System.out.println("URL为空");
}
}
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} catch(IOException ioEx) {
System.out.println("捕获到IOException:" + ioEx.toString());
} catch (Exception ex) {
System.out.println("意外异常");
ex.printStackTrace();
}
}
示例DatalinkSample.java将Oracle的URLhttps://www.oracle.com存储在表DATA_REPOSITORY中。然后,它会显示DATA_REPOSITORY中存储的URL所引用的所有文档的内容,包括Oracle主页https://www.oracle.com。
示例使用以下语句将URL作为java.net.URL对象从结果集中检索:
url = rs.getURL(2);
示例使用以下语句访问URL对象所引用的数据:
// 从URL检索内容
URLConnection myURLConnection = url.openConnection(proxy);
BufferedReader bReader =
new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
System.out.println("文档名称:" + documentName);
String pageContent = null;
while ((pageContent = bReader.readLine()) != null ) {
// 打印URL内容
System.out.println(pageContent);
}
方法URLConnection.openConnection可以不带参数,这意味着URLConnection表示直接连接到互联网。如果您需要代理服务器连接到互联网,则openConnection方法接受一个java.net.Proxy对象作为参数。以下语句演示如何创建一个HTTP代理,服务器名称为www-proxy.example.com,端口号为80:
代理 myProxy;
InetSocketAddress myProxyServer;
myProxyServer = new InetSocketAddress("www-proxy.example.com", 80);
myProxy = new 代理(代理.Type.HTTP, myProxyServer);