使用数据链接对象
原文: https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatalink.html
DATALINK
值通过 URL 引用基础数据源外部的资源。 URL,统一资源定位符,是指向万维网上的资源的指针。资源可以是文件或目录这样简单的东西,也可以是对更复杂的对象的引用,例如对数据库或搜索引擎的查询。
涵盖以下主题:
使用方法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 平台不支持方法getObject
或getURL
返回的 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 。
该示例使用以下语句从结果集中检索 URL 作为java.net.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);