{"id":35189,"date":"2024-11-26T12:41:19","date_gmt":"2024-11-26T04:41:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35189\/"},"modified":"2024-11-26T12:41:19","modified_gmt":"2024-11-26T04:41:19","slug":"java%e7%9a%84jdbc%e8%83%bd%e5%90%a6%e7%9b%b4%e6%8e%a5%e8%bf%94%e5%9b%9ehashmap%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35189\/","title":{"rendered":"Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173063489698204.jpg\" class=\"aligncenter\" title=\"Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f\u63d2\u56fe\" alt=\"Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>java\u7684jdbc\u80fd\u5426\u8fd4\u56dehashmap\uff1f<\/strong><\/p>\n<p>\u4e2d\u7684py\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570cursorclass\u8bbe\u7f6e\u8fd4\u56de\u7684\u67e5\u8be2\u7ed3\u679c\u96c6\u4e3a\u5b57\u5178\u7c7b\u578b\u3002\u90a3\u4e48\uff0cjava\u4e2d\u7684jdbc\u662f\u5426\u5b58\u5728\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\u4e3ahashmap\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>jdbc\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u4e3aresultsetrow\uff0c\u65e0\u6cd5\u76f4\u63a5\u8fd4\u56dehashmap\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u7ed3\u679c\u96c6\u8f6c\u6362\u4e3ahashmap\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u901a\u8fc7\u4ee3\u7406\u7c7b\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7ed3\u679c\u96c6\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>import java.sql.resultset;\nimport java.util.hashmap;\nimport java.util.map;\n\npublic class hashmapresultsetproxy implements resultset {\n    private resultset rs;\n\n    public hashmapresultsetproxy(resultset rs) {\n        this.rs = rs;\n    }\n\n    @override\n    public map&lt;string, object&gt; gethashmap() throws sqlexception {\n        hashmap&lt;string, object&gt; map = new hashmap&lt;&gt;();\n        int numcols = rs.getmetadata().getcolumncount();\n        for (int i = 0; i &lt; numcols; i++) {\n            map.put(rs.getmetadata().getcolumnname(i + 1), rs.getobject(i + 1));\n        }\n        return map;\n    }\n\n    \/\/ \u5176\u4ed6resultset\u5b9e\u73b0...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee3\u7406\u7c7b\u5305\u88c5jdbc\u63d0\u4f9b\u7684resultset\u8fd4\u56de\u65b0\u7ed3\u679c\u96c6\uff0c\u4ece\u800c\u8ba9jdbc\u652f\u6301\u8fd4\u56dehashmap\uff1a<\/li>\n<\/ul>\n<pre>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class JdbcHashMapDemo {\n\n    public static void main(String[] args) throws SQLException {\n        Connection conn = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/test\", \"user\", \"password\");\n        ResultSet rs = conn.createStatement().executeQuery(\"SELECT * FROM users\");\n\n        HashMapResultSetProxy hashMapResultSetProxy = new HashMapResultSetProxy(rs);\n        Map&lt;String, Object&gt; row = hashMapResultSetProxy.getHashMap();\n        System.out.println(row);\n\n        rs.close();\n        conn.close();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f\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>java\u7684jdbc\u80fd\u5426\u8fd4\u56dehashmap\uff1f \u4e2d\u7684py\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570cursorclass\u8bbe\u7f6e\u8fd4\u56de\u7684\u67e5\u8be2\u7ed3\u679c\u96c6\u4e3a\u5b57\u5178\u7c7b\u578b\u3002\u90a3\u4e48\uff0cjava\u4e2d\u7684jdbc\u662f\u5426\u5b58\u5728\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\u4e3ahashmap\uff1f \u7b54\u6848\uff1a jdbc\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u4e3aresultsetrow\uff0c\u65e0\u6cd5\u76f4\u63a5\u8fd4\u56dehashmap\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u7ed3\u679c\u96c6\u8f6c\u6362\u4e3ahashmap\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u901a\u8fc7\u4ee3\u7406\u7c7b\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7ed3\u679c\u96c6\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a import java.sql.resultset; import java.util.hashmap; import java.util.map; public class hashmapresultsetproxy implements resultset { private resultset rs; public hashmapresultsetproxy(resultset rs) { this.rs = rs; } @override public map&lt;string, object&gt; gethashmap() throws sqlexception { hashmap&lt;string, object&gt; map = new hashmap&lt;&gt;(); int numcols = rs.getmetadata().getcolumncount(); for (int i = 0; i &lt; numcols; [&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-35189","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35189","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=35189"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35189\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}