使用数据链接对象

原文: 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

  1. public void addURLRow(String description, String url)
  2. throws SQLException {
  3. PreparedStatement pstmt = null;
  4. try {
  5. pstmt = this.con.prepareStatement(
  6. "INSERT INTO data_repository" +
  7. "(document_name,url) VALUES (?,?)");
  8. pstmt.setString(1, description);
  9. pstmt.setURL(2,new URL(url));
  10. pstmt.execute();
  11. } catch (SQLException sqlex) {
  12. JDBCTutorialUtilities.printSQLException(sqlex);
  13. } catch (Exception ex) {
  14. System.out.println("Unexpected exception");
  15. ex.printStackTrace();
  16. } finally {
  17. if (pstmt != null) {
  18. pstmt.close();
  19. }
  20. }
  21. }

使用方法ResultSet.getURL将对外部数据的引用检索为java.net.URL对象。如果 Java 平台不支持方法getObjectgetURL返回的 URL 类型,请通过调用方法getString将 URL 检索为String对象。

以下示例 DatalinkSample.viewTable 显示存储在表DATA_REPOSITORY中的所有 URL 的内容:

  1. public static void viewTable(Connection con, Proxy proxy)
  2. throws SQLException, IOException {
  3. Statement stmt = null;
  4. String query =
  5. "SELECT document_name, url " +
  6. "FROM data_repository";
  7. try {
  8. stmt = con.createStatement();
  9. ResultSet rs = stmt.executeQuery(query);
  10. if ( rs.next() ) {
  11. String documentName = null;
  12. java.net.URL url = null;
  13. documentName = rs.getString(1);
  14. // Retrieve the value as a URL object.
  15. url = rs.getURL(2);
  16. if (url != null) {
  17. // Retrieve the contents
  18. // from the URL
  19. URLConnection myURLConnection =
  20. url.openConnection(proxy);
  21. BufferedReader bReader =
  22. new BufferedReader(
  23. new InputStreamReader(
  24. myURLConnection.
  25. getInputStream()));
  26. System.out.println("Document name: " + documentName);
  27. String pageContent = null;
  28. while ((pageContent = bReader.readLine()) != null ) {
  29. // Print the URL contents
  30. System.out.println(pageContent);
  31. }
  32. } else {
  33. System.out.println("URL is null");
  34. }
  35. }
  36. } catch (SQLException e) {
  37. JDBCTutorialUtilities.printSQLException(e);
  38. } catch(IOException ioEx) {
  39. System.out.println("IOException caught: " + ioEx.toString());
  40. } catch (Exception ex) {
  41. System.out.println("Unexpected exception");
  42. ex.printStackTrace();
  43. } finally {
  44. if (stmt != null) { stmt.close(); }
  45. }
  46. }

样例 DatalinkSample 将 Oracle URL http://www.oracle.com 存储在表DATA_REPOSITORY中。之后,它显示DATA_REPOSITORY中存储的 URL 引用的所有文档的内容,其中包括 Oracle 主页 http://www.oracle.com

该示例使用以下语句从结果集中检索 URL 作为java.net.URL对象:

  1. url = rs.getURL(2);

该示例使用以下语句访问URL对象引用的数据:

  1. URLConnection myURLConnection = url.openConnection(proxy);
  2. BufferedReader bReader = new BufferedReader(
  3. new InputStreamReader(
  4. myURLConnection.getInputStream()));
  5. System.out.println("Document name: " + documentName);
  6. String pageContent = null;
  7. while ((pageContent = bReader.readLine()) != null ) {
  8. // Print the URL contents
  9. System.out.println(pageContent);
  10. }

方法URLConnection.openConnection不带参数,这意味着URLConnection表示与 Internet 的直接连接。如果需要代理服务器连接到 Internet,openConnection方法接受java.net.Proxy对象作为参数。以下语句演示了如何使用服务器名称www-proxy.example.com和端口号80创建 HTTP 代理:

  1. Proxy myProxy;
  2. InetSocketAddress myProxyServer;
  3. myProxyServer = new InetSocketAddress("www-proxy.example.com", 80);
  4. myProxy = new Proxy(Proxy.Type.HTTP, myProxyServer);