{"id":203302,"date":"2025-05-22T09:51:23","date_gmt":"2025-05-22T01:51:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203302\/"},"modified":"2025-05-22T09:51:23","modified_gmt":"2025-05-22T01:51:23","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:\/\/server.hk\/cnblog\/203302\/","title":{"rendered":"Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aJava\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/173069842567285cb9a447b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>java\u7684jdbc\u80fd\u5426\u8fd4\u56dehashmap\uff1f<\/strong><\/p>\n<p>python\u4e2d\u7684pymysql\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<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<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>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deHashMap\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u7684JDBC\u80fd\u5426\u76f4\u63a5\u8fd4\u56deH&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203302","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203302"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203302\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}