{"id":38544,"date":"2024-11-26T13:38:47","date_gmt":"2024-11-26T05:38:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38544\/"},"modified":"2024-11-26T13:38:47","modified_gmt":"2024-11-26T05:38:47","slug":"java%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38544\/","title":{"rendered":"java\u600e\u4e48\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u8981\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u9700\u8981\u4f7f\u7528jdbc api\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u51c6\u5907sql\u8bed\u53e5\u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u96c6\u4ece\u7ed3\u679c\u96c6\u4e2d\u68c0\u7d22\u6570\u7ec4\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111503004893183.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u652f\u6301\u6570\u7ec4\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\u5e93\u548c JDBC\uff08Java \u6570\u636e\u5e93\u8fde\u63a5\uff09API\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528 JDBC \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Connection connection = DriverManager.getConnection(url, username, password);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u51c6\u5907 SQL \u8bed\u53e5\uff1a<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a PreparedStatement \u5bf9\u8c61\u6765\u6267\u884c\u67e5\u8be2\u3002<\/li>\n<li>\u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\uff08\u5982\u679c\u9700\u8981\uff09\u3002<\/li>\n<\/ul>\n<pre>String sql = \"SELECT array_column FROM table_name WHERE id = ?\";\nPreparedStatement statement = connection.prepareStatement(sql);\n\nint id = 1;\nstatement.setInt(1, id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u96c6\uff1a<\/strong><\/p>\n<pre>ResultSet resultSet = statement.executeQuery();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4ece\u7ed3\u679c\u96c6\u4e2d\u68c0\u7d22\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\u6839\u636e\u5217\u540d\u4ece ResultSet \u4e2d\u68c0\u7d22\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<pre>Array array = resultSet.getArray(\"array_column\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Array.getArray() \u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<pre>Object[] elements = (Object[]) array.getArray();\nfor (Object element : elements) {\n    System.out.println(element);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8981\u4ece PostgreSQL \u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>try (Connection connection = DriverManager.getConnection(\"jdbc:postgresql:\/\/localhost:5432\/database\", \"username\", \"password\")) {\n    String sql = \"SELECT array_column FROM table_name WHERE id = ?\";\n    PreparedStatement statement = connection.prepareStatement(sql);\n    statement.setInt(1, 1);\n    ResultSet resultSet = statement.executeQuery();\n    while (resultSet.next()) {\n        Array array = resultSet.getArray(\"array_column\");\n        Object[] elements = (Object[]) array.getArray();\n        for (Object element : elements) {\n            System.out.println(element);\n        }\n    }\n} catch (SQLException e) {\n    e.printStackTrace();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u4e0d\u540c\u7684\u6570\u636e\u5e93\u53ef\u80fd\u5bf9\u6570\u7ec4\u7684\u652f\u6301\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/li>\n<li>\u67d0\u4e9b\u6570\u636e\u5e93\u53ef\u80fd\u9700\u8981\u7279\u5b9a\u7684 JDBC \u7c7b\u578b\uff0c\u4f8b\u5982 PostgreSQL \u4e2d\u7684 ARRAY \u7c7b\u578b\u3002<\/li>\n<li>\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003\u6240\u7528\u6570\u636e\u5e93\u7684\u6587\u6863\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u83b7\u53d6\u6570\u636e\u5e93\u7684\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u9700\u8981\u4f7f\u7528jdbc api\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u51c6\u5907sql\u8bed\u53e5\u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u96c6\u4ece\u7ed3\u679c\u96c6\u4e2d\u68c0\u7d22\u6570\u7ec4\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20 \u5982\u4f55\u4f7f\u7528 Java \u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u7ec4 \u8981\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u652f\u6301\u6570\u7ec4\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\u5e93\u548c JDBC\uff08Java \u6570\u636e\u5e93\u8fde\u63a5\uff09API\u3002 \u6b65\u9aa4\uff1a 1. \u4f7f\u7528 JDBC \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Connection connection = DriverManager.getConnection(url, username, password); \u767b\u5f55\u540e\u590d\u5236 2. \u51c6\u5907 SQL \u8bed\u53e5\uff1a \u521b\u5efa\u4e00\u4e2a PreparedStatement \u5bf9\u8c61\u6765\u6267\u884c\u67e5\u8be2\u3002 \u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\uff08\u5982\u679c\u9700\u8981\uff09\u3002 String sql = &#8220;SELECT array_column FROM table_name WHERE id = ?&#8221;; PreparedStatement statement = connection.prepareStatement(sql); int id = 1; statement.setInt(1, id); \u767b\u5f55\u540e\u590d\u5236 3. \u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u96c6\uff1a ResultSet resultSet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38544","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38544"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38544\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}