{"id":61881,"date":"2024-10-13T14:25:14","date_gmt":"2024-10-13T06:25:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61881\/"},"modified":"2024-10-13T14:25:14","modified_gmt":"2024-10-13T06:25:14","slug":"java%e7%b7%a8%e5%af%ab%e7%9a%84%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8fjava","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61881\/","title":{"rendered":"Java\u7de8\u5beb\u7684\u9ad8\u6548\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f (\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8fjava)"},"content":{"rendered":"<h1 id=\"java%e7%b7%a8%e5%af%ab%e7%9a%84%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8fjava-PFuTLCuBSm\">Java\u7de8\u5beb\u7684\u9ad8\u6548\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f (\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8fjava)<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u7684\u958b\u767c\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Java\u4f5c\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u56e0\u5176\u5e73\u53f0\u7368\u7acb\u6027\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\uff0c\u6210\u70ba\u958b\u767c\u9ad8\u6548\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u7684\u7406\u60f3\u9078\u64c7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Java\u7de8\u5beb\u9ad8\u6548\u7684\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"java%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-PFuTLCuBSm\">Java\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>Java\u901a\u904eJava Database Connectivity (JDBC) API\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002JDBC\u63d0\u4f9b\u4e86\u4e00\u7d44\u6a19\u6e96\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97Java\u61c9\u7528\u7a0b\u5e8f\u80fd\u5920\u9023\u63a5\u5230\u5404\u7a2e\u4e0d\u540c\u7684\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JDBC\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\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}\n<\/code><\/pre>\n<h2 id=\"%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-PFuTLCuBSm\">\u9ad8\u6548\u7684\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u5728\u958b\u767c\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u6642\uff0c\u6027\u80fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u6548\u7387\u7684\u6280\u5de7\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528PreparedStatement\uff1a<\/strong>\u76f8\u8f03\u65bcStatement\uff0cPreparedStatement\u80fd\u5920\u63d0\u9ad8\u6027\u80fd\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u4e26\u4e14\u80fd\u5920\u9632\u6b62SQL\u6ce8\u5165\u3002<\/li>\n<li><strong>\u6279\u91cf\u8655\u7406\uff1a<\/strong>\u5c0d\u65bc\u5927\u91cf\u6578\u64da\u7684\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\uff0c\u4f7f\u7528\u6279\u91cf\u8655\u7406\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u9023\u63a5\u6c60\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u5275\u5efa\u548c\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\u7684\u958b\u92b7\u3002<\/li>\n<\/ul>\n<h3 id=\"%e4%bd%bf%e7%94%a8preparedstatement%e7%9a%84%e7%a4%ba%e4%be%8b-PFuTLCuBSm\">\u4f7f\u7528PreparedStatement\u7684\u793a\u4f8b<\/h3>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class InsertData {\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        String sql = \"INSERT INTO users (name, email) VALUES (?, ?)\";\n\n        try (Connection connection = DriverManager.getConnection(url, user, password);\n             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {\n\n            preparedStatement.setString(1, \"John Doe\");\n            preparedStatement.setString(2, \"john@example.com\");\n            preparedStatement.executeUpdate();\n            System.out.println(\"\u6578\u64da\u63d2\u5165\u6210\u529f\uff01\");\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96-PFuTLCuBSm\">\u6578\u64da\u5eab\u6027\u80fd\u512a\u5316<\/h2>\n<p>\u9664\u4e86\u7de8\u5beb\u9ad8\u6548\u7684\u4ee3\u78bc\u5916\uff0c\u6578\u64da\u5eab\u7684\u8a2d\u8a08\u548c\u914d\u7f6e\u4e5f\u5c0d\u6027\u80fd\u6709\u8457\u91cd\u8981\u5f71\u97ff\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u512a\u5316\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u7d22\u5f15\uff1a<\/strong>\u70ba\u5e38\u7528\u7684\u67e5\u8a62\u5b57\u6bb5\u5275\u5efa\u7d22\u5f15\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<li><strong>\u6b63\u898f\u5316\uff1a<\/strong>\u901a\u904e\u6b63\u898f\u5316\u6578\u64da\u5eab\u8a2d\u8a08\uff0c\u6e1b\u5c11\u6578\u64da\u5197\u9918\uff0c\u63d0\u9ad8\u6578\u64da\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u67e5\u8a62\u512a\u5316\uff1a<\/strong>\u4f7f\u7528EXPLAIN\u8a9e\u53e5\u5206\u6790\u67e5\u8a62\u8a08\u5283\uff0c\u627e\u51fa\u6027\u80fd\u74f6\u9838\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-PFuTLCuBSm\">\u7d50\u8ad6<\/h2>\n<p>Java\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u548c\u5eab\u4f86\u958b\u767c\u9ad8\u6548\u7684\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u3002\u901a\u904e\u5408\u7406\u4f7f\u7528JDBC\u3001PreparedStatement\u4ee5\u53ca\u512a\u5316\u6578\u64da\u5eab\u8a2d\u8a08\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u6301\u7e8c\u95dc\u6ce8\u6027\u80fd\u512a\u5316\u5c07\u662f\u958b\u767c\u8005\u7684\u91cd\u8981\u4efb\u52d9\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u6258\u7ba1\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u5728\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u74b0\u5883\u4e2d\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Java\u7de8\u5beb\u9ad8\u6548\u7684\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\uff0c\u5be6\u73fe\u6700\u4f73\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61881","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61881","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=61881"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61881\/revisions"}],"predecessor-version":[{"id":61882,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61881\/revisions\/61882"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}