{"id":6611,"date":"2024-11-14T09:39:34","date_gmt":"2024-11-14T01:39:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6611\/"},"modified":"2024-11-14T09:39:34","modified_gmt":"2024-11-14T01:39:34","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93url%e6%80%8e%e4%b9%88%e5%86%99-mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e6%80%8e%e4%b9%88%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6611\/","title":{"rendered":"mysql\u6570\u636e\u5e93url\u600e\u4e48\u5199 mysql\u6570\u636e\u5e93\u600e\u4e48\u63d2\u5165\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  mysql \u6570\u636e\u5e93 url \u7f16\u5199\uff1a\u683c\u5f0f\u4e3a jdbc:mysql:\/\/[hostname]:[port]\/[database name]\uff0c\u5176\u4e2d hostname \u4e3a\u670d\u52a1\u5668\u5730\u5740\uff0cport \u4e3a\u76d1\u542c\u7aef\u53e3\uff08\u9ed8\u8ba4 3306\uff09\uff0cdatabase name \u4e3a\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002mysql \u6570\u636e\u5e93\u6570\u636e\u63d2\u5165\uff1a\u52a0\u8f7d jdbc \u9a71\u52a8\u7a0b\u5e8f\u3002\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3002\u521b\u5efa preparedstatement\u3002\u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\u3002\u6267\u884c\u67e5\u8be2\u4ee5\u63d2\u5165\u6570\u636e\u3002\u5173\u95ed\u8d44\u6e90\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/13\/2024081313404355008.jpg\" class=\"aligncenter\" title=\"mysql\u6570\u636e\u5e93url\u600e\u4e48\u5199 mysql\u6570\u636e\u5e93\u600e\u4e48\u63d2\u5165\u6570\u636e\u63d2\u56fe\" alt=\"mysql\u6570\u636e\u5e93url\u600e\u4e48\u5199 mysql\u6570\u636e\u5e93\u600e\u4e48\u63d2\u5165\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u6570\u636e\u5e93 URL \u7f16\u5199<\/strong><\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199 MySQL \u6570\u636e\u5e93 URL\uff1f<\/strong><\/p>\n<p>MySQL \u6570\u636e\u5e93 URL \u7684\u683c\u5f0f\u4e3a\uff1a<\/p>\n<pre>jdbc:mysql:\/\/[hostname]:[port]\/[database name]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>hostname \u662f MySQL \u670d\u52a1\u5668\u7684\u5730\u5740\uff0c\u53ef\u4ee5\u662f IP \u5730\u5740\u6216\u57df\u540d\u3002<\/li>\n<li>port \u662f MySQL \u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7\uff0c\u9ed8\u8ba4\u4e3a 3306\u3002<\/li>\n<li>database name \u662f\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8981\u8fde\u63a5\u5230\u4f4d\u4e8e localhost \u4e0a\u4e14\u547d\u540d\u4e3a mydb \u7684\u6570\u636e\u5e93\uff0c\u5176 URL \u4e3a\uff1a<\/p>\n<pre>jdbc:mysql:\/\/localhost:3306\/mydb<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>MySQL \u6570\u636e\u5e93\u6570\u636e\u63d2\u5165<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5411 MySQL \u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 JDBC\uff08Java \u6570\u636e\u5e93\u8fde\u63a5\uff09API \u5411 MySQL \u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li> <strong>\u52a0\u8f7d JDBC \u9a71\u52a8\u7a0b\u5e8f\uff1a<\/strong>\u9996\u5148\uff0c\u9700\u8981\u52a0\u8f7d MySQL JDBC \u9a71\u52a8\u7a0b\u5e8f\uff1a<\/li>\n<\/ol>\n<pre>Class.forName(\"com.mysql.cj.jdbc.Driver\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/strong>\u4f7f\u7528 DriverManager \u7c7b\u5efa\u7acb\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff1a<\/li>\n<\/ol>\n<pre>Connection connection = DriverManager.getConnection(jdbcUrl, username, password);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u521b\u5efa PreparedStatement\uff1a<\/strong>\u4e3a\u8981\u6267\u884c\u7684\u63d2\u5165\u67e5\u8be2\u521b\u5efa\u4e00\u4e2a PreparedStatement\uff1a<\/li>\n<\/ol>\n<pre>PreparedStatement statement = connection.prepareStatement(\"INSERT INTO table_name (column1, column2, ...) VALUES (?, ?, ...)\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\uff1a<\/strong>\u4f7f\u7528 setXXX \u65b9\u6cd5\u8bbe\u7f6e PreparedStatement \u4e2d\u7684\u53c2\u6570\u503c\uff1a<\/li>\n<\/ol>\n<pre>statement.setString(1, value1);\nstatement.setInt(2, value2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u6267\u884c\u67e5\u8be2\uff1a<\/strong>\u6267\u884c PreparedStatement \u4ee5\u63d2\u5165\u6570\u636e\uff1a<\/li>\n<\/ol>\n<pre>int rowCount = statement.executeUpdate();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u5173\u95ed\u8d44\u6e90\uff1a<\/strong>\u6700\u540e\uff0c\u5173\u95ed PreparedStatement \u548c\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/li>\n<\/ol>\n<pre>statement.close();\nconnection.close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93url\u600e\u4e48\u5199 mysql\u6570\u636e\u5e93\u600e\u4e48\u63d2\u5165\u6570\u636e\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>mysql \u6570\u636e\u5e93 url \u7f16\u5199\uff1a\u683c\u5f0f\u4e3a jdbc:mysql:\/\/[hostname]:[port]\/[database name]\uff0c\u5176\u4e2d hostname \u4e3a\u670d\u52a1\u5668\u5730\u5740\uff0cport \u4e3a\u76d1\u542c\u7aef\u53e3\uff08\u9ed8\u8ba4 3306\uff09\uff0cdatabase name \u4e3a\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002mysql \u6570\u636e\u5e93\u6570\u636e\u63d2\u5165\uff1a\u52a0\u8f7d jdbc \u9a71\u52a8\u7a0b\u5e8f\u3002\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\u3002\u521b\u5efa preparedstatement\u3002\u8bbe\u7f6e\u67e5\u8be2\u53c2\u6570\u3002\u6267\u884c\u67e5\u8be2\u4ee5\u63d2\u5165\u6570\u636e\u3002\u5173\u95ed\u8d44\u6e90\u3002 MySQL \u6570\u636e\u5e93 URL \u7f16\u5199 \u5982\u4f55\u7f16\u5199 MySQL \u6570\u636e\u5e93 URL\uff1f MySQL \u6570\u636e\u5e93 URL \u7684\u683c\u5f0f\u4e3a\uff1a jdbc:mysql:\/\/[hostname]:[port]\/[database name] \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a hostname \u662f MySQL \u670d\u52a1\u5668\u7684\u5730\u5740\uff0c\u53ef\u4ee5\u662f IP \u5730\u5740\u6216\u57df\u540d\u3002 port \u662f MySQL \u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7\uff0c\u9ed8\u8ba4\u4e3a 3306\u3002 database name \u662f\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u7684\u540d\u79f0\u3002 \u793a\u4f8b\uff1a \u8981\u8fde\u63a5\u5230\u4f4d\u4e8e localhost \u4e0a\u4e14\u547d\u540d\u4e3a mydb \u7684\u6570\u636e\u5e93\uff0c\u5176 URL \u4e3a\uff1a jdbc:mysql:\/\/localhost:3306\/mydb [&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-6611","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6611","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=6611"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6611\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}