{"id":167857,"date":"2024-11-06T07:59:18","date_gmt":"2024-11-05T23:59:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167857\/"},"modified":"2024-11-06T07:59:19","modified_gmt":"2024-11-05T23:59:19","slug":"%e9%80%a3%e6%8e%a5%e8%88%87%e6%9b%b4%e6%96%b0%ef%bc%9a%e8%bc%95%e9%ac%86%e7%ae%a1%e7%90%86%e6%95%b8%e6%93%9a%e5%ba%ab-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8c%e6%9b%b4%e6%96%b0%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167857\/","title":{"rendered":"\u9023\u63a5\u8207\u66f4\u65b0\uff1a\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab (\u9023\u63a5\u6578\u64da\u5eab\u548c\u66f4\u65b0\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e9%80%a3%e6%8e%a5%e8%88%87%e6%9b%b4%e6%96%b0%ef%bc%9a%e8%bc%95%e9%ac%86%e7%ae%a1%e7%90%86%e6%95%b8%e6%93%9a%e5%ba%ab-MsgQlkcIwm\">\u9023\u63a5\u8207\u66f4\u65b0\uff1a\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u4f01\u696d\u9084\u662f\u5927\u578b\u4f01\u696d\uff0c\u6578\u64da\u5eab\u90fd\u662f\u5b58\u5132\u548c\u7ba1\u7406\u4fe1\u606f\u7684\u6838\u5fc3\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u9023\u63a5\u6578\u64da\u5eab\u4ee5\u53ca\u5982\u4f55\u9032\u884c\u6578\u64da\u66f4\u65b0\uff0c\u5e6b\u52a9\u7528\u6236\u8f15\u9b06\u7ba1\u7406\u5176\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-MsgQlkcIwm\">\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u6578\u64da\u5eab\u9023\u63a5\u662f\u6307\u61c9\u7528\u7a0b\u5e8f\u8207\u6578\u64da\u5eab\u4e4b\u9593\u7684\u901a\u4fe1\u904e\u7a0b\u3002\u9019\u4e00\u904e\u7a0b\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528\u7279\u5b9a\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u4f86\u5efa\u7acb\u9023\u63a5\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5305\u62ec MySQL\u3001PostgreSQL\u3001SQLite \u548c Microsoft SQL Server \u7b49\u3002<\/p>\n<h3 id=\"%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a5%e9%a9%9f-MsgQlkcIwm\">\u9023\u63a5\u6578\u64da\u5eab\u7684\u6b65\u9a5f<\/h3>\n<ul>\n<li><strong>\u9078\u64c7\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\uff1a<\/strong>\u6839\u64da\u6240\u4f7f\u7528\u7684\u6578\u64da\u5eab\u985e\u578b\u9078\u64c7\u5408\u9069\u7684\u9a45\u52d5\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc MySQL\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>mysql-connector-java<\/code>\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u9023\u63a5\u53c3\u6578\uff1a<\/strong>\u9019\u4e9b\u53c3\u6578\u901a\u5e38\u5305\u62ec\u6578\u64da\u5eab\u7684 URL\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc MySQL\uff0c\u9023\u63a5\u5b57\u7b26\u4e32\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/li>\n<\/ul>\n<pre><code>jdbc:mysql:\/\/localhost:3306\/your_database?user=your_username&amp;password=your_password<\/code><\/pre>\n<ul>\n<li><strong>\u5efa\u7acb\u9023\u63a5\uff1a<\/strong>\u4f7f\u7528\u9a45\u52d5\u7a0b\u5e8f\u7684 API \u4f86\u5efa\u7acb\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Java \u9023\u63a5 MySQL \u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/li>\n<\/ul>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    public static void main(String[] args) {\n        String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n        String user = \"your_username\";\n        String password = \"your_password\";\n\n        try {\n            Connection connection = DriverManager.getConnection(url, user, password);\n            System.out.println(\"\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff01\");\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-MsgQlkcIwm\">\u6578\u64da\u5eab\u66f4\u65b0\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u9032\u884c\u6578\u64da\u7684\u66f4\u65b0\u3002\u6578\u64da\u66f4\u65b0\u901a\u5e38\u6d89\u53ca\u5230 SQL \u8a9e\u53e5\u7684\u57f7\u884c\uff0c\u6700\u5e38\u898b\u7684\u66f4\u65b0\u64cd\u4f5c\u662f\u4f7f\u7528 <code>UPDATE<\/code> \u8a9e\u53e5\u3002<\/p>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e7%9a%84%e6%ad%a5%e9%a9%9f-MsgQlkcIwm\">\u66f4\u65b0\u6578\u64da\u7684\u6b65\u9a5f<\/h3>\n<ul>\n<li><strong>\u7de8\u5beb SQL \u66f4\u65b0\u8a9e\u53e5\uff1a<\/strong>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>users<\/code> \u7684\u8868\uff0c\u6211\u5011\u60f3\u8981\u66f4\u65b0\u7528\u6236\u7684\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\uff1a<\/li>\n<\/ul>\n<pre><code>UPDATE users SET email = 'new_email@example.com' WHERE id = 1;<\/code><\/pre>\n<ul>\n<li><strong>\u57f7\u884c\u66f4\u65b0\u64cd\u4f5c\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u5c0d\u8c61\u4f86\u57f7\u884c SQL \u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Java \u57f7\u884c\u66f4\u65b0\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/li>\n<\/ul>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class UpdateDatabase {\n    public static void main(String[] args) {\n        String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n        String user = \"your_username\";\n        String password = \"your_password\";\n\n        try {\n            Connection connection = DriverManager.getConnection(url, user, password);\n            Statement statement = connection.createStatement();\n            String sql = \"UPDATE users SET email = 'new_email@example.com' WHERE id = 1;\";\n            int rowsAffected = statement.executeUpdate(sql);\n            System.out.println(\"\u66f4\u65b0\u4e86 \" + rowsAffected + \" \u884c\u3002\");\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-MsgQlkcIwm\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u9023\u63a5\u548c\u66f4\u65b0\u6642\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\u53ef\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u548c\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\uff1a<\/strong>\u9019\u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\u653b\u64ca\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code>PreparedStatement preparedStatement = connection.prepareStatement(\"UPDATE users SET email = ? WHERE id = ?;\");\npreparedStatement.setString(1, \"new_email@example.com\");\npreparedStatement.setInt(2, 1);\npreparedStatement.executeUpdate();<\/code><\/pre>\n<ul>\n<li><strong>\u5b9a\u671f\u5099\u4efd\u6578\u64da\uff1a<\/strong>\u5728\u9032\u884c\u4efb\u4f55\u66f4\u65b0\u4e4b\u524d\uff0c\u78ba\u4fdd\u5099\u4efd\u6578\u64da\uff0c\u4ee5\u9632\u6b62\u610f\u5916\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li><strong>\u76e3\u63a7\u6027\u80fd\uff1a<\/strong>\u4f7f\u7528\u6578\u64da\u5eab\u6027\u80fd\u76e3\u63a7\u5de5\u5177\u4f86\u78ba\u4fdd\u6578\u64da\u5eab\u904b\u884c\u9806\u66a2\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MsgQlkcIwm\">\u7e3d\u7d50<\/h2>\n<p>\u9023\u63a5\u548c\u66f4\u65b0\u6578\u64da\u5eab\u662f\u6578\u64da\u7ba1\u7406\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002\u901a\u904e\u6b63\u78ba\u7684\u9023\u63a5\u65b9\u6cd5\u548c\u66f4\u65b0\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002\u7121\u8ad6\u60a8\u662f\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u4e86\u89e3\u9019\u4e9b\u57fa\u672c\u6982\u5ff5\u5c07\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u5229\u7528\u6578\u64da\u5eab\u8cc7\u6e90\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u7528\u6236\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u65b9\u6848\u5c07\u662f\u660e\u667a\u4e4b\u8209\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u9023\u63a5\u8207\u66f4\u65b0\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u8b93\u60a8\u7684\u696d\u52d9\u904b\u884c\u66f4\u9806\u66a2\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-167857","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167857","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=167857"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167857\/revisions"}],"predecessor-version":[{"id":167858,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167857\/revisions\/167858"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}