{"id":6476,"date":"2024-11-14T10:00:02","date_gmt":"2024-11-14T02:00:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6476\/"},"modified":"2024-11-14T10:00:02","modified_gmt":"2024-11-14T02:00:02","slug":"mysql%e6%80%8e%e4%b9%88%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6476\/","title":{"rendered":"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 mysql \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u53ea\u9700\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u3002\u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\u3002\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/05\/2024080508190285748.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528 MySQL \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61<\/strong><\/p>\n<p>\u8981\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a Connection \u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>Connection connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/database_name\", \"username\", \"password\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>&#8220;jdbc::\/\/localhost:3306\/database_name&#8221; \u662f MySQL \u6570\u636e\u5e93\u7684 JDBC URL\uff0c\u5305\u542b\u4e3b\u673a\u540d\u3001\u7aef\u53e3\u53f7\u548c\u6570\u636e\u5e93\u540d\u79f0\u3002<\/li>\n<li>&#8220;username&#8221; \u548c &#8220;password&#8221; \u662f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2<\/strong><\/p>\n<p>\u4e00\u65e6\u5efa\u7acb\u4e86\u8fde\u63a5\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 Statement \u6216 PreparedStatement \u5bf9\u8c61\u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\u3002\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u7ed3\u679c\uff1a<\/p>\n<pre>Statement statement = connection.createStatement();\nResultSet resultSet = statement.executeQuery(\"SELECT * FROM table_name\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>resultSet \u5bf9\u8c61\u5305\u542b\u67e5\u8be2\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<pre>while (resultSet.next()) {\n    int id = resultSet.getInt(\"id\");\n    String name = resultSet.getString(\"name\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/p>\n<p>\u5728\u5b8c\u6210\u67e5\u8be2\u540e\uff0c\u8bf7\u52a1\u5fc5\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>connection.close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u4f7f\u7528\u8fde\u63a5\u6c60\uff1a<\/strong> \u8fde\u63a5\u6c60\u662f\u4e00\u79cd\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/li>\n<li> <strong>\u5904\u7406\u5f02\u5e38\uff1a<\/strong> \u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u59cb\u7ec8\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002<\/li>\n<li> <strong>\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong> \u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u4f8b\u5982\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 mysql \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u53ea\u9700\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u3002\u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\u3002\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u6dfb\u52a0\u6570\u636e\u5e93\u8fde\u63a5 \u4f7f\u7528 MySQL \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\u5373\u53ef\uff1a 1. \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61 \u8981\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a Connection \u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a Connection connection = DriverManager.getConnection(&#8220;jdbc:mysql:\/\/localhost:3306\/database_name&#8221;, &#8220;username&#8221;, &#8220;password&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a &#8220;jdbc::\/\/localhost:3306\/database_name&#8221; \u662f MySQL \u6570\u636e\u5e93\u7684 JDBC URL\uff0c\u5305\u542b\u4e3b\u673a\u540d\u3001\u7aef\u53e3\u53f7\u548c\u6570\u636e\u5e93\u540d\u79f0\u3002 &#8220;username&#8221; \u548c &#8220;password&#8221; \u662f\u8fde\u63a5\u5230\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002 2. \u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2 \u4e00\u65e6\u5efa\u7acb\u4e86\u8fde\u63a5\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 Statement \u6216 PreparedStatement \u5bf9\u8c61\u6267\u884c\u6570\u636e\u5e93\u67e5\u8be2\u3002\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u7ed3\u679c\uff1a Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(&#8220;SELECT * FROM table_name&#8221;); \u767b\u5f55\u540e\u590d\u5236 resultSet \u5bf9\u8c61\u5305\u542b\u67e5\u8be2\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\uff1a while (resultSet.next()) [&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-6476","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6476","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=6476"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6476\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}